Kivy:无法从 python 代码 "AttrErr: 'super' object has no attribute '__getattr__" 添加按钮

Kivy: Can't add button from python code "AttrErr: 'super' object has no attribute '__getattr__"

我知道这个问题被问过多次,但这些解决方案对我没有帮助,或者我真的不明白。

我想在 python "for loop" 的帮助下添加数字按钮,但是当我这样做时出现以下错误:

"self.ids.GoodsContainer.add_widget(Button(text="嗨")) 文件 "kivy\properties.pyx",第 863 行,在 kivy.properties.ObservableDict.getattr 中 AttributeError: 'super' 对象没有属性 'getattr'"

我卡在这里好久了

每当我删除 'GoodsView()'(python 文件)中的按钮部分时,程序运行完美。所以我可以自己在 "kv rule" 中输入数字,但稍后我想添加由函数生成的标签,所以我现在需要找到一种方法,否则我会遇到更多问题。

py:

class ActionBar(ActionBar):
    pass


class Manager(ScreenManager):
    pass


class Screen_one(Screen):
    pass


class Screen_two(Screen):
    pass


class GoodsView(ScrollView):
    def __init__(self, **kwargs):
        super(GoodsView, self).__init__(**kwargs)
        for i in range(10):
            self.ids.GoodsContainer.add_widget(Button(text="hi"))


class Screen_three(Screen):
    pass


class RandApp(App):
    def build(self):
        return Builder.load_file("pcapp.kv")


if __name__ == "__main__":
    RandApp().run()

kv:

BoxLayout:
    orientation: 'vertical'
    canvas.before:
        Color:
            rgba: .65, .75, .85, 1
        Rectangle:
            pos: self.pos
            size: self.size

    ActionBar: ...
    Manager:
        id: sm
        Screen_one:
            ...
        Screen_two:
            ...
        Screen_three:
            ...
<Screen_one>:
    FloatLayout:
        Button:
            text: "Click1"
            size_hint: .2, .05
            pos_hint: {'x': .2, 'y': .4}
            on_release:
                app.root.ids.sm.current  = 'screen2'
        Label:
            text: 'Hello!'
            pos_hint: {'x': -0.2, 'y': 0}

<Screen_two>:
    GoodsView:


<GoodsView>:
    id:gv
    do_scroll_x: False
    do_scroll_y: True
    size_hint_x: .7
    size_hint_y: .7
    pos_hint: {'x': .15,'y': .15}
    canvas.before:
        Color:
            rgba: 1, 0, 0, 1
        Rectangle:
            size: self.size
            pos: self.pos

    GridLayout:
        id: GoodsContainer
        row_force_default: True
        row_default_height: '50dp'
        cols: 1
        spacing: 10
        padding: 10

编辑1 错误:

Traceback (most recent call last):
   File "kivy\properties.pyx", line 860, in kivy.properties.ObservableDict.__getattr__
 KeyError: 'GoodsContainer'

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "C:/Users/shokh/Desktop/PhytonSaves/Calculator.py", line 44, in <module>
     CalculatorApp().run()
   File "C:\Users\shokh\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\app.py", line 829, in run
     root = self.build()
   File "C:/Users/shokh/Desktop/PhytonSaves/Calculator.py", line 40, in build
     return Builder.load_file("pcapp.kv")
   File "C:\Users\shokh\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\lang\builder.py", line 301, in load_file
     return self.load_string(data, **kwargs)
   File "C:\Users\shokh\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\lang\builder.py", line 405, in load_string
     rule_children=rule_children)
   File "C:\Users\shokh\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\lang\builder.py", line 659, in _apply_rule
     child, crule, rootrule, rule_children=rule_children)
   File "C:\Users\shokh\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\lang\builder.py", line 657, in _apply_rule
     root=rctx['ids']['root'], rule_children=rule_children)
   File "C:\Users\shokh\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\widget.py", line 469, in apply_class_lang_rules
     rule_children=rule_children)
   File "C:\Users\shokh\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\lang\builder.py", line 538, in apply
     rule_children=rule_children)
   File "C:\Users\shokh\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\lang\builder.py", line 654, in _apply_rule
     child = cls(__no_builder=True)
   File "C:/Users/shokh/Desktop/PhytonSaves/Calculator.py", line 31, in __init__
     self.ids.GoodsContainer.add_widget(Button(text="hi"))
   File "kivy\properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'

问题是在 __init__ 期间尚未填充 ids 字典,因为所有其他小部件尚未创建并相互添加。

尝试做类似

的事情
from kivy.clock import Clock

# ...and the following in your class

def __init__(self, *args **kwargs):
    super().__init__(*args, **kwargs)
    Clock.schedule_once(self.post_init, 0)

def post_init(self, dt):
    # your code goes here

这会将您的代码安排在下一帧 运行,在所有内容都初始化之后。