我有一个像这样的python脚本:
class GuiApp(App):
def build(self):
#for i in range(24):
#Grid.add_widget(Button(text='Test'))
return Gui()
class Gui(BoxLayout):
pass
我有一个像这样的.kv文件:
<Gui>:
BoxLayout:
orientation: 'vertical'
Button:
text: 'Top'
GridLayout:
id: Grid
cols: 5
rows: 5
如何应用循环将24个按钮添加到GridLayout?
我认为我可以像python注释中所示调用id Grid,但是失败了。
如何应用循环将按钮添加到带有id Grid的kv文件中的GridLayout?
答案 0 :(得分:4)
我似乎已经弄清楚如何进行循环操作:
吡啶
class GuiApp(App):
def build(self)
g = Gui()
for i in range(24):
g.grid.add_widget(Button(text='test'))
return g
class Gui(BoxLayout):
grid = ObjectProperty(None)
KV
<Gui>:
grid: Grid
BoxLayout:
orientation: 'vertical'
Button:
text: 'Top'
GridLayout:
id: Grid
cols: 5
rows: 5
为了使它能够工作,我需要引用它_grid:在.kv文件中的Grid,由ObjectProperty找到,网格在python中使用时需要小写
答案 1 :(得分:2)
def build(self):
layout = GridLayout()
for i in range(24): layout.add_widget(...)
return layout
我认为至少
class GuiApp(App):
def build(self):
g = Gui()
for i in range(24):
g.Grid.add_widget(Button(text='Test'))
return g
class Gui(BoxLayout):
Grid = ObjectProperty(None)