在GridLayout轴中添加多个GridLayout

时间:2019-06-12 18:32:05

标签: python kivy

我希望能够将GridLayouts动态添加到单个GridLayout容器中。

我的代码:

layout = GridLayout(cols=1, size_hint= (None,None))
...
get_score = data.get(key, {}).get('score')
                can = self.root.get_screen("three")

                for x in range(-1, get_score): # had to use -1 to get correct amount of shapes
                    can.ids.my_box.add_widget(layout)

kv:


        GridLayout:
            cols: 1
            id: my_box
            size_hint_y: None
            height: self.minimum_height
            row_force_default: True
            row_default_height: 50

尝试执行此操作时出现错误

 kivy.uix.widget.WidgetException: Cannot add <kivy.uix.gridlayout.GridLayout object at 0x00000274FEFD9B40>, it already has a parent <kivy.uix.gridlayout.GridLayout object at 0x00000274FEFD99A0>

1 个答案:

答案 0 :(得分:0)

您试图在循环中多次添加相同的小部件(名为layout的GridLayout)。尝试将layout = GridLayout(cols=1, size_hint= (None,None))行放在for循环中,然后每次它将是一个不同的小部件,并且代码应运行。