The Wayback Machine - https://web.archive.org/web/20240709171919/https://www.geeksforgeeks.org/radiobutton-in-tkinter-python/
Open In App

RadioButton in Tkinter | Python

Last Updated : 16 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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: 
Radio buttons, 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. 
 

Python3




# 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 separately
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()


Output: 
 

The background of these button boxes is light blue. Button boxes having a white backgrounds as well as sunken are selected ones.
 

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

Python3




# 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 separately
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()


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. 
 

Python3




# 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 separately
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()


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.
 



Previous Article
Next Article

Similar Reads

Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications. Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the 'ttk' module, which enhances the visual appeal and functionality of the applications. In this article, we will cover the differences, providing a
4 min read
PyQt5 - How to set skin to indicator of RadioButton ?
In this article we will see how we can set skin to the indicator of the radio button. Radio button has basically two parts one is indicator and the other one is label part which show text. Skin is a background image which adjust it self according to the size of indicator. In order to set skin to the indicator we have to change the style sheet of th
2 min read
PyQt5 – Setting skin to unchecked RadioButton
In this article we will see how we can set skin to the unchecked check box. By default there is no image/skin associated with the radio button, this skin will appear when radio button will be in checked state. In order to add skin to the radio button for checked state, we have to change the style sheet of the radio button. Below is the style sheet
2 min read
PyQt5 – Background image of checked RadioButton indicator when pressed
In this article we will see how we can set background image to the indicator of radio button when it get pressed and was in checked state. By default when we pressed the radio button there is bluish color associated with it although we can change it. The background image only appear to the indicator when it was in checked state and when we pressed
2 min read
PyQt5 – Setting skin to RadioButton indicator when mouse hover
In this article we will see how we can set skin to the indicator of radio button when mouse hover over it. By default no image is associated to the indicator when mouse hover over it, although we can set skin to it. Skin is basically a background image which adjust it self according to the size of indicator. In order to do this we have to change th
2 min read
PyQt5 – Skin of checked RadioButton indicator when pressed
In this article we will see how we can set skin to the indicator of radio button when it get pressed and was in checked state. By default when we pressed the radio button there is bluish color associated with it although we can change it. The skin only appear to the indicator when it was in checked state and when we pressed the radio button. In ord
2 min read
PyQt5 – How to set Skin unchecked RadioButton indicator when pressed
In this article we will see how we can set skin to the indicator of radio button when it get pressed and was in unchecked state. By default when we pressed the radio button there is bluish color associated with it although we can change it. The skin only appear to the indicator when it was in unchecked state and when we pressed the radio button. In
2 min read
PyQt5 – How to set Skin to unchecked RadioButton indicator when mouse hover
In this article we will see how we can set skin to the indicator of radio button when mouse hover over it and was in unchecked state. By default no skin or image associated with it although we can set skin to it. The skin only appear to the indicator when it was in unchecked state and when mouse hover over the radio button. In order to add skin to
2 min read
PyQt5 – How to set Skin to checked RadioButton indicator when mouse hover
In this article we will see how we can set skin to the indicator of radio button when mouse hover over it and was in checked state. By default no skin or image associated with it although we can set skin to it. The skin only appear to the indicator when it was in checked state and when mouse hover over the radio button. In order to add skin to indi
2 min read
PyQt5 - Setting skin to checked indicator of RadioButton
In this article we will see how we can see skin to indicator of radio button when it is in checked state. By default a black circle is associated to the checked state although we can add skin to it.Skin is basically a background image which adjust itself according to the size of indicator. This skin only appear when radio button will be in checked
2 min read
three90RightbarBannerImg