按下按鈕時出現問題:我總是得到下面的錯誤。誰能建議我如何解決這個問題?主.py文件:class MainApp(MDApp): def build(self): self.dati = Builder.load_file("conf.kv") return Builder.load_file("conf.kv") def show_data(self): print(self.boxlay.btn_nav.scr1.classe.text)MainApp().run()conf.kv 文件:BoxLayout: orientation:'vertical' id: boxlay btn_nav:btn_nav MDToolbar: title: 'Bottom navigation' MDBottomNavigation: id: btn_nav scr1:scr1 MDBottomNavigationItem: id: scr1 classe:classe name: 'screen 1' text: 'Python' icon: 'language-python' MDTextField: id: classe hint_text: "Enter Class" pos_hint:{'center_x': 0.5, 'center_y': 0.5} size_hint_x:None width:300 MDRectangleFlatButton: text: 'Python' pos_hint: {'center_x': 0.5, 'center_y': 0.4} on_release: app.show_data()運行此代碼我收到的錯誤是: on_release: app.show_data() File "main.py", line 27, in show_data print( AttributeError: 'NoneType' object has no attribute 'btn_nav') AttributeError: 'BoxLayout' object has no attribute 'classe'感謝您的幫助
1 回答

慕少森
TA貢獻2019條經驗 獲得超9個贊
由于您已經ids定義,您可以在 python 代碼中使用它們來訪問從您的kv. 所以show_data()方法可以是:
def show_data(self):
print(self.root.ids.classe.text)
另外,我注意到你在打電話:
Builder.load_file("conf.kv")
在你的方法中兩次build()。雖然這不是錯誤,但它可能不是您想要的。該行:
self.dati = Builder.load_file("conf.kv")
創建由以下行創建的 GUI 的完整副本:
return Builder.load_file("conf.kv")
但是, 所引用的小部件樹self.dati不是您的 GUI 中的小部件樹,因此self.dati可能沒有價值。我懷疑你的build()方法應該是:
def build(self):
self.dati = Builder.load_file("conf.kv")
return self.dati
添加回答
舉報
0/150
提交
取消