Using radio button widgets

In this recipe, we will create three tkinter Radiobutton widgets. We will also add some code that changes the color of the main form depending upon which Radiobutton is selected.

Getting ready

This recipe extends the previous recipes.

How to do it...

We are adding the following code to the previous recipe:

# Radiobutton Globals # 1 COLOR1 = "Blue" # 2 COLOR2 = "Gold" # 3 COLOR3 = "Red" # 4 # Radiobutton Callback # 5 def radCall(): # 6 radSel=radVar.get() if radSel == 1: win.configure(background=COLOR1) elif radSel == 2: win.configure(background=COLOR2) elif radSel == 3: win.configure(background=COLOR3) # create three Radiobuttons # 7 radVar = tk.IntVar() # 8 rad1 = tk.Radiobutton(win, text=COLOR1, variable=radVar, value=1, ...

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.