如何解决AttributeError:“ BoxLayout”对象没有属性,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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。