【发布时间】:2012-12-11 02:31:55
【问题描述】:
所以我正在尝试重新创建这个:
self.button1 = tkinter.Button(self.user_frame, image = self.image1, state = 'disabled', command = self.press1)
self.button1.pack(side = 'left')
self.buttonList.append(self.button1)
self.button2 = tkinter.Button(self.user_frame, image = self.image2, state = 'disabled', command = self.press2)
self.button2.pack(side = 'left')
self.buttonList.append(self.button2)
self.button3 = tkinter.Button(self.user_frame, image = self.image3, state = 'disabled', command = self.press3)
self.button3.pack(side = 'left')
self.buttonList.append(self.button3)
等..直到self.button10。
有了这个:
for x in range [1, 11]:
self.button(x)=tkinter.Button(self.user_frame, image=self.image(x), state = 'disabled', command = self.press(x))
self.button(x).pack(side='left')
self.buttonList.append(self.button(x))
我收到语法错误:无法分配给函数调用。我知道这与我如何命名按钮有关,但我无法弄清楚。有什么建议吗?
【问题讨论】:
-
你可能想看看
setattr,它可以让你以你所说的方式声明变量(我没有任何 Tkinter 经验,所以我不确定是否有一个更适合该环境的解决方案。 -
只需使用按钮对象列表。
标签: python loops for-loop assign