Python Kivy中遍历内部的字符串连接

Python Kivy中遍历内部的字符串连接,python,kivy,Python,Kivy,我认为这是一个语法问题;我试图在遍历中添加一个动态元素(?可能是错误的单词),它将许多按钮的文本添加到列表中。这基本上是伪代码,但我的想法是: list = [self.root.ids.('button_' + i).text for i in range (5)] 这是为了避免我有以下问题: list = [] list.append(self.root.ids.button_1.text) list.append(self.root.ids.button_2.text) list.app

我认为这是一个语法问题;我试图在遍历中添加一个动态元素(?可能是错误的单词),它将许多按钮的文本添加到列表中。这基本上是伪代码,但我的想法是:

list = [self.root.ids.('button_' + i).text for i in range (5)]
这是为了避免我有以下问题:

list = []
list.append(self.root.ids.button_1.text)
list.append(self.root.ids.button_2.text)
list.append(self.root.ids.button_3.text)
list.append(self.root.ids.button_4.text)
list.append(self.root.ids.button_5.text)

有可能这样做吗?
按钮是应该是动态的位。如果我这里的任何术语不正确,我深表歉意。

要动态获取属性,您必须使用
getattr(obj,'attr\u name')

在您的情况下,可能是这样的:

getattr(self.root.ids, 'button_%d' % i).text

有关更多详细信息,请参阅。

getattr(self.root.ids,'button_%d'%i')。如果
i
是整数,则文本
,如果不使用
%s
。这完全正确,我如何将其标记为正确答案?我现在要读一些关于getattr()的文章,谢谢。我把它作为一个答案贴了出来。