我正在尝试使用 id 函数来引用 MDTextField 中的 text_input,但是我无法理解 id 函数是如何工作的。有谁知道我的代码有什么问题?第一个代码是主应用程序,第二个代码是所有小部件所在的位置。我已经在互联网上搜索了解决方案,但我不明白为什么我的代码不起作用。from kivy.lang import Builderfrom kivymd.app import MDAppfrom kivy.uix.screenmanager import Screen, ScreenManagerfrom Screen_helper import Homeclass MenuScreen(Screen): passclass ProfileScreen(Screen): passsm = ScreenManager()sm.add_widget(MenuScreen(name='Menu'))sm.add_widget(MenuScreen(name='Profile'))class Mainapp(MDApp): def build(self): screen = Screen() helper = Builder.load_string(Home) screen.add_widget(helper) key = self.root.ids.username_input return screenMainapp().run()Home = '''ScreenManager: MenuScreen: ProfileScreen:<MenuScreen>: name: 'Menu' MDRectangleFlatButton: id: my_button text: 'Profile' pos_hint: {'center_x': 0.5, 'center_y': 0.1} on_press: root.manager.current = 'Profile' MDTextField: id: username_input input_filter: "int" hint_text: 'CHIAVE NUMERICA' helper_text: 'compresa tra 0 e 95' helper_text_mode: 'on_focus' icon_right: 'key-variant' icon_right_color: app.theme_cls.primary_color pos_hint: {'center_x':0.5,'center_y':0.55} size_hint_x:None width:230 input_filter: 'int' <ProfileScreen>: name: 'Profile' MDLabel: text: 'Welcome' halign: 'center' MDRectangleFlatButton: text: 'back' pos_hint: {'center_x': 0.5, 'center_y': 0.3} on_press: root.manager.current = 'Menu' '''
1 回答
![?](http://img1.sycdn.imooc.com/545847d40001cbef02200220-100-100.jpg)
小怪兽爱吃肉
TA贡献1852条经验 获得超1个赞
为 MenuScreen 类提供一个 id,然后访问该小部件。
将其添加到 kv 文件中。
<MenuScreen>: id: menu
要访问该小部件,您现在可以在 python 中执行以下操作:
key = self.root.menu.ids.username_input
添加回答
举报
0/150
提交
取消