【发布时间】:2018-03-26 01:40:02
【问题描述】:
我正在练习使用 for 循环在 Python 中创建 Tkinter 小部件。但是如果在某些情况下我需要处理在 for 循环下创建的小部件,我该怎么做呢?
另外,我对下面的 for 循环的实际工作方式感到困惑,因为看起来只有一个单选按钮对象是由三个具有不同属性的副本创建的。谁能解释一下我们如何使用相同的对象名称创建三个实例背后的逻辑?
import tkinter as tk
#Window definition
win = tk.Tk()
win.geometry("500x500")
#Radio Button Globals
colors = ["Blue", "Gold", "Red"]
#Radio Button click action
def radCall():
radSEL = radVar.get()
if radSEL == 0:
win.configure(background=COLOR1)
elif radSEL == 1:
win.configure(background=COLOR2)
elif radSEL == 2:
win.configure(background=COLOR3)
#Radio Button
radVar = tk.IntVar() #Radio Button Variable
radVar.set(99) #Select non existened value
for col in range(3):
rad = tk.Radiobutton(win, text=colors[col], variable=radVar, value=col, command=radCall)
rad.grid(column=col, row=5, sticky=tk.W)
win.mainloop()
【问题讨论】:
-
你做过研究吗?这个网站上有很多关于在循环中创建小部件的问题。
标签: python loops for-loop tkinter widget