繁体   English   中英

Python/KivyMD:如何从 python/kivymd 中 MDScreen 内的 MDTextField 打印文本

[英]Python/KivyMD: How to print the text from a MDTextField that is inside an MDScreen in python/kivymd

嗨,我是 kivymd 的新手,我正在尝试打印用户在按下按钮时在屏幕内的 MDTextField 中插入的文本。 通常我会使用self.screen.ids.name_of_id.text来访问 .py 文件中的文本,但是这在使用屏幕时似乎不起作用并给出错误AttributeError: 'super' object has no attribute ' getattr ' . 有没有办法从 .py 文件中访问文本?

我的 .kv 片段

ScreenManager:
    ForgotPassword:
    Login:
    ResetPassword:
    Main:

<ForgotPassword@Screen>
    name: "forgot"
    MDFloatLayout:

        MDTextField:
            id: forgot_email
            hint_text: "Email"
            size_hint: .7, None
            pos_hint: {"center_x": .5}

        MDFillRoundFlatButton:
            text: "                Requesitar Nova Password                "
            md_bg_color: 0, 0.4, 1, 1
            text_color: 1, 1, 1, 1
            font_size: 16
            pos_hint: {"center_x": .5, "center_y": .15}
            on_press: app.forgot_password()

我的 .py 代码

class MainApp(MDApp):
    def build(self):
        self.theme_cls.primary_palette = "Blue"
        self.theme_cls.theme_style = "Light"
        self.theme_cls.primary_hue = "500"
        self.screen = Builder.load_file("Backup.kv")
        return self.screen

    def forgot_password(self):
        print(self.screen.ids.forgot_email.text)



MainApp().run()

print()语句中的self.screenScreenManager ,在您的情况下,它将没有ids ,从而导致错误。 ids仅在kv中每个规则的根中可用。 因此,在您的kv ,只有ScreenManagerForgotPassword可以有ids 但是ScreenManager没有定义ids ,所以它没有ids forgot_email是在ForgotPassword规则下定义的,因此该id将出现在ForgotPassword Screen实例的ids中。 因此,要访问该MDTextField ,请尝试:

def forgot_password(self):
    print(self.screen.get_screen('forgot').ids.forgot_email.text)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM