KivyMD、Kivy、ScreenManager、TextField不改变文字

KivyMD, Kivy, ScreenManager, TextField do not change the text

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

my.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()

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

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'