[英]How can i get the text of an MDTextField (Or Data "Login") of an screen if im in another Screen. (Kivy / KivyMD / Python)
[英]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.screen
是ScreenManager
,在您的情况下,它将没有ids
,从而导致错误。 ids
仅在kv
中每个规则的根中可用。 因此,在您的kv
,只有ScreenManager
和ForgotPassword
可以有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.