【问题标题】:KivyMD, Kivy, ScreenManager, TextField do not change the textKivyMD、Kivy、ScreenManager、TextField 不改变文本
【发布时间】:2021-07-04 17:29:53
【问题描述】:

当我尝试从我的 TextField 中获取 Text 值时,我总是会空着。同时,如果我设置默认文本值,例如“123”,那么无论我是否在控制台的 TextField 中输入任何内容,我仍然会得到“123”。我猜测这可能是由于某种屏幕重复,但是当我调用 self.root.get_screen ('registration') 时。 ID,我得到三个不同的 id,即没有重复。我会很高兴你的帮助

我的.kv

<RegistrationScreen>:
    name: "registration"
    MDCard:
        size_hint: None, None
        size: 400, 600
        orientation: "vertical"
        pos_hint: {"center_x": 0.5, "center_y": 0.5}

        padding: 15
        spacing: 50

        MDLabel:
            text: "Регистрация"
            font_name: 'fonts/montserrat-bold.ttf'
            font_size: 20

            color: .43, 0, .48, 1
            halign: "center"

        BoxLayout:
            size_hint: None, None
            size: 200, 160
            pos_hint: {"center_x": 0.5}
            orientation: "vertical"

            MDTextField:
                id: pomogite

                hint_text: "Rectangle mode"
                mode: "rectangle"
                helper_text_mode: "on_focus"

                hint_text: "Введите логин"
                helper_text: "Минимум 6 символов (a-z, A-Z, 0-9)"
                icon_right: "account"

                color_mode: 'custom'
                line_color_focus: .43, 0, .48, 1

                size_hint_x: None
                width: 250

                pos_hint: {"center_x": .5, "center_y": .3}
                text: "Начинайте ввод"

            MDTextField:
                id: textfield_password

                hint_text: "Rectangle mode"
                mode: "rectangle"
                helper_text_mode: "on_focus"

                hint_text: "Введите пароль"
                helper_text: "Минимум 6 символов (a-z, A-Z, 0-9)"
                icon_right: "form-textbox-password"

                color_mode: 'custom'
                line_color_focus: .43, 0, .48, 1

                size_hint_x: None
                width: 250

                pos_hint: {"center_x": .5, "center_y": .3}

        MDRectangleFlatButton:
            id: reg
            text: "Регистрация"
            theme_text_color: "Custom"
            text_color: .43, 0, .48, 1
            line_color: .43, 0, .48, 1
            pos_hint: {"center_x": .5}
            on_press: app.registration()

main.py

from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.app import MDApp
from client import Client
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.textfield import MDTextField


sm = ScreenManager()


class LoadingScreen(Screen):
    pass


class AuthorizationScreen(Screen):
    pass


class RegistrationScreen(Screen):
    pass



class MyApp(MDApp):

    def build(self):

        sm.add_widget(LoadingScreen(name='loading'))
        sm.add_widget(AuthorizationScreen(name='authorization'))
        sm.add_widget(RegistrationScreen(name='registration'))
        sm.switch_to(AuthorizationScreen())

        return sm

    def fff(self):
        self.screen.ids.text_field_error.error = True
        sm.switch_to(LoadingScreen())

    def registration(self):
        addwindow_instance = self.root.get_screen('registration')

        print(addwindow_instance.ids)
        print(addwindow_instance.ids["pomogite"].text)



MyApp().run()

【问题讨论】:

    标签: python kivy kivy-language kivymd


    【解决方案1】:

    您的代码中有几个错误,当您使用类名后跟() 时,您正在创建该类的新实例。所以这行:

    sm.switch_to(AuthorizationScreen())
    

    创建AuthorizationScreen 的一个新实例,除了该行已经创建的实例:

    sm.add_widget(AuthorizationScreen(name='authorization'))
    

    更好的方法是使用sm.current = 而不是sm.switch_to,如下所示:

    def build(self):
        sm.add_widget(LoadingScreen(name='loading'))
        sm.add_widget(AuthorizationScreen(name='authorization'))
        sm.add_widget(RegistrationScreen(name='registration'))
        sm.current = 'authorization'
        return sm
    

    这会将当前屏幕切换到已经存在的AuthorizationScreen 实例。或者,更简单,只需将AuthorizationScreen 作为第一个添加的Screen,它将成为当前的Screen

    def build(self):
        sm.add_widget(AuthorizationScreen(name='authorization'))
        sm.add_widget(LoadingScreen(name='loading'))
        sm.add_widget(RegistrationScreen(name='registration'))
        return sm
    

    同样的错误出现在您的fff() 方法中:

    sm.switch_to(LoadingScreen())
    

    这是创建LoadingScreen 的新实例,而不是使用已经存在的实例。那行应该是:

    sm.current = 'loading'
    

    【讨论】:

    • 哦,是的,感谢您的帮助。我最初还认为屏幕以某种方式重复,但我无法弄清楚问题所在。毕竟在显示self.root.get_screen('registration')的时候。 ids收到了三个不同的id,为什么?再次感谢
    • 因为你在kv中定义了三个id。
    猜你喜欢
    • 1970-01-01
    • 2018-05-25
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多