Python Tkinter section

Radio Button Select and Deselect

Post by Amina Delali, January 27th, 2023

Description

I think you already know what is a RadioButton widget. The one in Python Tkinter is not different. It is a widget that you use to make a selection. So, you select the widget, and you deselect it. But, the difference between the radiobutton and a usual check box, is that you use the radio button in groups. Which means that you don't use only one radio button.You use it paired with other ones. And when you select one of them, the other ones must automatically get deselected.

To create the pairing between the radio buttons, you simply use the same variable as the variable property.
And to set the default values; or in other words, to specify which radio button will be selected first, you initialize its value property, with the same value of the common variable. For the other ones, you set different values.
And of course, if you initialize all the radio buttons to the same value as their variable value, they will be all selected.
In the following I use a StringVar, initialized by the string "purple". It will correspond to the default background color of themain window.

How to set the default value of a radio button python tkinter ? - png  code picture

In the code below, I will use two radio buttons to change the background color of the main window. To do this, I assigned to their command property the function toggleColor. The function will:

  • Get and assign the value of the common variable of the radio buttons (myVal) to the background color of the main window.
  • Assign the same value to certain color properties of the radio buttons.
  • Change the foreground color of the radio buttons (fg property) according to the new background color, just for contrast reasons.



The Code

from tkinter import *
from tkinter import ttk

main_window=Tk()
main_window.title("Purple And Gold ")
main_window.geometry('500x250+400+100'
main_window.configure(bg="purple")

def toggleColor():
        colorVal myVal.get()
        main_window["bg"]=colorVal
        myRBPurp["bg"]=colorVal
        myRBPurp["selectcolor"]=colorVal
        myRBGold["bg"]=colorVal
        myRBGold["selectcolor"]=colorVal
        if colorVal=="purple":
                myRBPurp["fg"]="white"
                myRBGold["fg"]="white"
        else:
                myRBPurp["fg"]="black"
                myRBGold["fg"]="black"  

myValStringVar(main_window,"purple")
myRBPurp Radiobutton(main_window,text="Purple",variable=myVal
                              value"purple",
                              bg="purple",fg="white",selectcolor="purple",
                              command=toggleColor,
                              font=("Musicals",15,"bold"))                            
myRBPurp.pack(padx=10pady=20side="right",anchor="ne")

myRBGold Radiobutton(main_window,text="Gold"variable=myVal,
                            value="gold",
                            bg="purple",fg="white",selectcolor="purple",
                            command=toggleColor,
                            font=("Musicals",15,"bold"))                            
myRBGold.pack(padx=10pady=20side="top",anchor="ne",)

main_window.mainloop()

The Output

Something to say?

If you want to add something about this post, please feel free to do it by commenting below 🙂.