![](/img/trans.png)
[英]AttributeError: 'module' object has no attribute 'textinput'
[英]AttributeError: 'BoxLayout' object has no attribute, textinput
[已修改] 按下按钮时出现问题:总是出现以下错误。 谁能建议我如何解决这个问题?
main.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'
谢谢你的帮助
由于您定义了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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.