New Tutorials:   TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL
See the Tutorial List

Python Tkinter Radiobutton Widget

In this tutorial, we will cover the Tkinter Radiobutton widget in Python, which is used when we want to add a radio button to the GUI of our application.

Tkinter radiobutton widget is used to implement multiple-choice options that are mainly created in user input forms.

  • This widget offers multiple selections to the user and allows the user to select only one option from the given ones. Thus it is also known as implementing one-of-many selection in a Python Application.

  • Also, different methods can also be associated with radiobutton.

  • You can also display multiple line text and images on the radiobutton.

  • Each radiobutton displays a single value for a particular variable.

  • You can also keep a track of the user's selection of the radiobutton because it is associated with a single variable

Tkinter Radiobutton Widget

The syntax of the Radiobutton widget is given below:

W = Radiobutton(master, options)   

In the above syntax, the master parameter denotes the parent window. You can use many options to change the look of the radiobutton and these options are written as comma-separated key-value pairs.

Tkinter Radiobutton Widget Options:

Following are the options used with Tkinter Radiobutton widgets:

Name of the option Description
anchor This option is used to represent the exact position of the text within the widget, in the case of the widget contains more space than the requirement of the text. The default value of this option is CENTER.
bg This option represents the background color of the widget.
activebackground This option represents the background color of the widget when it is under focus.
activeforeground This option represents the font color of the widget when it is under focus.
borderwidth This option is used to represent the size of the border.
bitmap If you want to display graphics on the widget then you can set this widget to any graphical or image object.
command This option is used to set the procedure which must be called every time when the state of the radiobutton is changed.
cursor This option will convert the mouse pointer to the specified cursor type and it can be set to an arrow, dot, etc.
font This option is used to represent the font type of the text of the widget.
fg This option is used to represent the foreground color of the text of the widget.
height This option indicates the vertical dimension of the widget
width This option indicates the horizontal dimension of the widget and it is represented as the number of characters.
padx This option represents the horizontal padding of the widget.
pady This option represents the vertical padding of the widget
highlightcolor This option is used to represent the color of the focus highlight when the widget is under the focus
highlightbackground This option is used to represent the color of the focus highlight when the widget is not under the focus.
image If you want to display an image on the widget then this option will be set to an image rather than the text
justify This option is used to represent the justification of the multiline text. The default value is CENTER. Other values are LEFT, RIGHT.
relief This option is used to represent the type of border. The default value is FLAT.
selectcolor This option indicates the color of the radiobutton when it is selected
selectimage This option indicates the image to be displayed on the radiobutton when it is selected
state This option is used to represent the state of the radio button. The default state of the Radiobutton is NORMAL. You can also set the state to DISABLED in order to make the radiobutton unresponsive.
text This option indicates the text to be displayed on the radiobutton.
textvariable This option is used to control the text represented by the widget. The textvariable can be set to the text that is needed to be shown on the widget.
underline This option can be set to an existing number in order to specify that nth letter of the string will be underlined. Its default value is -1 which indicates no underline
variable This option is also known as the control variable which is used to keep the track of user's choices. Thus this variable is shared among all radiobuttons.
value This option of each radiobutton is assigned to the control variable when it is turned on by the user.
wraplength This option is used to wrap the text to the number of lines just by setting this option to the desired number so that each line contains only that number of characters.

Tkinter Radiobutton Widget Methods:

Following are the various methods used with the Tkinter Radiobutton widgets:

Method Name Description
deselect() This method is used to deselect or turns off the radio button
select() This method is used to select the radio button
invoke() This method is generally used to call a function when the state of radio button gets changed.
flash() This method is generally used to flash the radio button between its normal and active colors many times.

Tkinter Radiobutton Widget Example

Below we have a basic example for the radio button widget. let us see the code snippet for the Radiobutton widget:

#firstly ImportTkinter module 
from tkinter import * 
from tkinter.ttk import *

# Creating parent Tkinter window 
win = Tk() 
win.geometry("200x200") 

# let us create a Tkinter string variable 
# that is able to store any string value 
v = StringVar(win, "1") 

# here is a Dictionary to create multiple buttons 
options = {" Option A" : "1", 
		"Option B" : "2", 
		"Option C" : "3", 
		"Option D" : "4" 
		} 

# We will use a Loop just to create multiple 
# Radiobuttons instaed of creating each button separately
for (txt, val) in options.items(): 
	Radiobutton(win, text=txt, variable=v, value=val).pack(side = TOP, ipady = 4) 

mainloop() 

The above code will give the following output:

Note: If you will try above code snippet by yourself then you will see that in the output you can select only one button at a time.

Tkinter Radiobutton Widget Another Example

Below we have another example of this widget where we will add styling to radiobutton using style class:

from tkinter import * 
from tkinter.ttk import *

win= Tk() 
win.geometry('200x200') 
v = StringVar(win, "1") 

# we will add Style class to add style to Radiobutton  
style = Style(win) 
style.configure("TRadiobutton", background = "light blue", 
				foreground = "orange", font = ("arial", 14, "bold")) 

# Dictionary to create multiple buttons 
values = {"RadioButton 1" : "1", 
		"RadioButton 2" : "2", 
		"RadioButton 3" : "3" 
		} 

for (text, value) in values.items(): 
	Radiobutton(win, text = text, variable = v, 
				value = value).pack(side = TOP, ipady = 3) 
mainloop() 

The above code will change the font style as well as background and foreground colors. In the above TRadiobutton is used in style class, which automatically applies styling to all the available Radiobuttons.

Summary:

In this tutorial, we learned about the Radiobutton widget which is used to create multiple options out of which a single can be selected. This is mainly used when we create a user form, like registration form.