Kivy:无法从python代码“ AttrErr:'super'对象没有属性'__getattr__”添加按钮

时间:2019-07-20 08:37:03

标签: python kivy kivy-language

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

我想借助python“ for loop”添加数字按钮,但是当我这样做时,出现以下错误:

“ self.ids.GoodsContainer.add_widget(Button(text =” hi“))    在kivy.properties.ObservableDict中,文件“ kivy \ properties.pyx”,行863, getattr  AttributeError:“超级”对象没有属性“ getattr ””

我在这里待了很长时间。

每当我删除“ GoodsView()”(python文件)中的按钮部分时,porgram就会完美运行。因此,我可以自己在“ kv规则”中键入数字,但后来我想添加由函数生成的标签,所以我现在需要找到一种方法,否则我将遇到更多问题。

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__'

1 个答案:

答案 0 :(得分:1)

问题在于__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

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