簡體   English   中英

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

我的.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()

主文件

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

代碼中有幾個錯誤,每當您使用類名后跟() ,您就是在創建該類的新實例。 所以這一行:

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'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM