【问题标题】:Python/KivyMD: How to print the text from a MDTextField that is inside an MDScreen in python/kivymdPython/KivyMD:如何从 python/kivymd 中的 MDScreen 内的 MDTextField 打印文本
【发布时间】:2020-10-05 19:55:43
【问题描述】:

您好,我对 kivymd 还很陌生,我正在尝试打印用户在按下按钮时在屏幕内的 MDTextField 中插入的文本。通常我会使用 self.screen.ids.name_of_id.text 来访问 .py 文件中的文本,但是在使用屏幕时这似乎不起作用并给出错误 AttributeError:“超级”对象没有属性“getattr。有没有办法从 .py 文件中访问文本?

我的 .kv sn-p

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

【问题讨论】:

    标签: python kivy kivymd


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 2021-06-16
      • 1970-01-01
      • 2023-03-13
      相关资源
      最近更新 更多