Adding several widgets in a loop

So far we have created several widgets of the same type (for example. Radiobutton) by basically copying and pasting the same code and then modifying the variations (for example, the column number). In this recipe, we start refactoring our code to make it less redundant.

Getting ready

We are refactoring some parts of the previous recipe's code, so you need that code to apply to this recipe to.

How to do it...

# First, we change our Radiobutton global variables into a list. colors = ["Blue", "Gold", "Red"] # 1 # create three Radiobuttons using one variable radVar = tk.IntVar() Next we are selecting a non-existing index value for radVar. radVar.set(99) # 2 Now we are creating all three Radiobutton widgets within one loop. ...

Get Python GUI Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.