RadioButton in Tkinter | Python

The Radiobutton is a standard Tkinter widget used to implement one-of-many selections. Radiobuttons can contain text or images, and you can associate a Python function or method with each button. When the button is pressed, Tkinter automatically calls that function or method.

Syntax:

button = Radiobutton(master, text=”Name on Button”, variable = “shared variable”, value = “values of each button”, options = values, …)



shared variable = A Tkinter variable shared among all Radio buttons
value = each radiobutton should have different value otherwise more than 1 radiobutton will get selected.

Code #1:

Radiobuttons, but not in the form of buttons, in form of button box. In order to display button box indicatoron/indicator option should be set to 0.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing Tkinter module
from tkinter import *
# from tkinter.ttk import *
  
# Creating master Tkinter window
master = Tk()
master.geometry("175x175")
  
# Tkinter string variable
# able to store any string value
v = StringVar(master, "1")
  
# Dictionary to create multiple buttons
values = {"RadioButton 1" : "1",
          "RadioButton 2" : "2",
          "RadioButton 3" : "3",
          "RadioButton 4" : "4",
          "RadioButton 5" : "5"}
  
# Loop is used to create multiple Radiobuttons
# rather than creating each button seperately
for (text, value) in values.items():
    Radiobutton(master, text = text, variable = v, 
                value = value, indicator = 0,
                background = "light blue").pack(fill = X, ipady = 5)
  
# Infinite loop can be terminated by
# keyboard or mouse interrupt
# or by any predefined function (destroy())
mainloop()

chevron_right


Output:

Background of these button boxes are light blue. Button boxes having white background as well as sunken are selected ones.


 
Code #2: Changing button boxes into standard radio buttons. For this remove indicatoron option.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing Tkinter module
from tkinter import * from tkinter.ttk import *
  
# Creating master Tkinter window
master = Tk()
master.geometry("175x175")
  
# Tkinter string variable
# able to store any string value
v = StringVar(master, "1")
  
# Dictionary to create multiple buttons
values = {"RadioButton 1" : "1",
          "RadioButton 2" : "2",
          "RadioButton 3" : "3",
          "RadioButton 4" : "4",
          "RadioButton 5" : "5"}
  
# Loop is used to create multiple Radiobuttons
# rather than creating each button seperately
for (text, value) in values.items():
    Radiobutton(master, text = text, variable = v,
        value = value).pack(side = TOP, ipady = 5)
  
# Infinite loop can be terminated by
# keyboard or mouse interrupt
# or by any predefined function (destroy())
mainloop()

chevron_right


Output:

These Radiobuttons are created using tkinter.ttk that is why background option is not available but we can use style class to do styling.
 

Code #3: Adding Style to Radio Button using style class.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing Tkinter module
from tkinter import * from tkinter.ttk import *
  
# Creating master Tkinter window
master = Tk()
master.geometry('175x175')
  
# Tkinter string variable
# able to store any string value
v = StringVar(master, "1")
  
# Style class to add style to Radiobutton
# it can be used to style any ttk widget
style = Style(master)
style.configure("TRadiobutton", background = "light green"
                foreground = "red", font = ("arial", 10, "bold"))
  
# Dictionary to create multiple buttons
values = {"RadioButton 1" : "1",
          "RadioButton 2" : "2",
          "RadioButton 3" : "3",
          "RadioButton 4" : "4",
          "RadioButton 5" : "5"}
  
# Loop is used to create multiple Radiobuttons
# rather than creating each button seperately
for (text, value) in values.items():
    Radiobutton(master, text = text, variable = v, 
                value = value).pack(side = TOP, ipady = 5)
  
# Infinite loop can be terminated by
# keyboard or mouse interrupt
# or by any predefined function (destroy())
mainloop()

chevron_right


Output:

You may observe that font style is changed as well as background and foreground colors are also changed. Here, TRadiobutton is used in style class, it automatically applies styling to all the available Radiobuttons.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.