我正在嘗試使用 id 函數(shù)來引用 MDTextField 中的 text_input,但是我無法理解 id 函數(shù)是如何工作的。有誰(shuí)知道我的代碼有什么問題?第一個(gè)代碼是主應(yīng)用程序,第二個(gè)代碼是所有小部件所在的位置。我已經(jīng)在互聯(lián)網(wǎng)上搜索了解決方案,但我不明白為什么我的代碼不起作用。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 回答

小怪獸愛吃肉
TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊
為 MenuScreen 類提供一個(gè) id,然后訪問該小部件。
將其添加到 kv 文件中。
<MenuScreen>: id: menu
要訪問該小部件,您現(xiàn)在可以在 python 中執(zhí)行以下操作:
key = self.root.menu.ids.username_input
添加回答
舉報(bào)
0/150
提交
取消