如何动态引用kivy小部件?

时间:2015-11-30 18:45:00

标签: python kivy

试图引用带有变量的多个小部件。例如:

#.kv
Button:
    id: button1

Button:
    id: button2

Button:
    id: button2

#.py
for x in range(3):
    self.ids.button[x].text = "This is button " + x

1 个答案:

答案 0 :(得分:0)

使用self.ids是dict(+ getattr stuff)这一事实可能是最简单的:

for x in range(3):
    self.ids['button{}'.format(x)].text = 'This is button {}'.format(x)

您可能也可以使用getattr:

for x in range(3):
    getattr(self.ids, 'button{}'.format(x)).text = 'This is button {}'.format(x)