Creating several tkinter widgets in a for loop and assigning them to variables Creating several tkinter widgets in a for loop and assigning them to variables tkinter tkinter

Creating several tkinter widgets in a for loop and assigning them to variables


The line curRad = 'rad' + str(col) accomplishes absolutely nothing, since the variable gets reassigned on the next line.

The code does not give you any lasting reference to the individual radio buttons - but you don't normally need one: determining which one is selected, or programmatically selecting one, is done via the variable (radVar) that they all share.

If you really wanted to keep a reference to each button, you could put:

allRads = []

above the loop, and:

allRads.append(curRad)

inside the loop.


I really like your solution, jason. You can reference later the buttons through the indexes of the list generated.

You can also create the buttons as follow

for col in range(3):                              globals()["curRad" + str(col)] = tk.Radiobutton(win, text=colors[col], variable=radVar, value=col, command=radCall)

So the name of the variables associated to each button will be different, in this case:

curRad0

curRad1

curRad2