【问题标题】:How can i access object properties of another class in kivy without getting AttributeError: 'super' object has no attribute '__getattr__'如何在不获取 AttributeError 的情况下访问 kivy 中另一个类的对象属性:'super' object has no attribute '__getattr__'
【发布时间】:2018-02-15 17:40:25
【问题描述】:

您好,我试图使用 self.ids 从不同的类访问对象的属性。但我收到了这个烦人的错误 AttributeError: 'super' object has no attribute '__getattr__' 这是我的代码,当我单击“男孩按钮”时出现错误

.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"

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    如果我们观察到Get_Boys 的子元素是button_blabel_b,那么这些元素就是可以通过ids 访问的元素。但是如果我们看Get_PeopleGet_Boys是一个孩子,所以你可以通过parent方法访问Get_People,然后通过root_lbl访问:

    class Get_Boys(BoxLayout):
         label_b=ObjectProperty()
    
         def show(self):
            self.parent.root_lbl.text='i am called'
    

    【讨论】:

    • 很高兴它像魔术一样工作,谢谢,有什么地方可以提前了解
    猜你喜欢
    • 1970-01-01
    • 2022-10-13
    • 2023-01-22
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    相关资源
    最近更新 更多