我如何在不获取AttributeError的情况下访问kivy中另一个类的对象属性:'super'对象没有属性'__getattr__' [英] How can i access object properties of another class in kivy without getting AttributeError: 'super' object has no attribute '__getattr__'
问题描述
我试图使用self.ids从其他类访问对象的属性.但是我收到此烦人的错误 AttributeError:'super'对象没有属性'__getattr __' 这是我的代码,当我单击男生按钮"时出现错误
Hi i was trying to access the property of an object from a different class using self.ids.. but i get this annoying error AttributeError: 'super' object has no attribute '__getattr__' Here is my Code when i click the "Button for boys" i get the error
.py文件
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
class Get_People(BoxLayout):
root_lbl=ObjectProperty()
class Get_Boys(BoxLayout):
label_b=ObjectProperty()
def show(self):
self.ids. root_lbl.text='i am called'
class lstApp(App):
def build(self):
self.load_kv('dates_test.kv')
return Get_People()
if __name__=='__main__':
lstApp().run()
这是.Kv文件
<Get_People>:
root_lbl: root_lbl
orientation: 'vertical'
Button:
name: root_btn
id: root_btn
text: "I am Root Button"
Label:
id: root_lbl
text: "I am Root Label"
Get_Boys:
<Get_Boys>:
label_b: label_b
Button:
id: button_b
text: "Button for boys"
on_press: root.show()
Label:
id: label_b
text: "Label for boys"
推荐答案
如果我们观察到,我们发现Get_Boys
的子元素是button_b
和label_b
,因此它们是可以通过ids
访问的元素.但是,如果我们查看Get_People
,则Get_Boys
是一个孩子,因此您可以通过parent
方法访问Get_People
,然后通过root_lbl
访问:
If we observe we see that the children of Get_Boys
are button_b
and label_b
so those are the elements that can be accessed through ids
. But if we look in Get_People
, Get_Boys
is a child so you can access Get_People
through the parent
method, and then access through root_lbl
:
class Get_Boys(BoxLayout):
label_b=ObjectProperty()
def show(self):
self.parent.root_lbl.text='i am called'
这篇关于我如何在不获取AttributeError的情况下访问kivy中另一个类的对象属性:'super'对象没有属性'__getattr__'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!