Python Tkinter RadioButton

In this tutorial, we will learn about Python Tkinter RadioButton. Tkinter RadioButton is used to show multiple options to the user from these options user can select only one option. We can associate a different method with each RadioButton. Furthermore, we can show multiple lines or images with RadioButtons.

It is important to note, to add proper functionality in RadioButtons each RadioButton must be associated with a single variable. Each RadioButton must symbolize with a single value for that particular variable.

Syntax of Python Tkinter RadioButton:

w = Radiobutton (master, options)  
  • masterThis represents the parent window.
  • options: Here is the list of most commonly used options for this widget.

Example of Python Tkinter RadioButton:

# importing tkinter lib
from tkinter import *  

l= ["C", "Java", "Python"]

def choice():  
    selection = "You have chosen : " + l[radio.get()]  
    message.config(text = selection)  
    
mainWindow = Tk()  

mainWindow.geometry("320x150")

radio = IntVar()  

lb = Label(text = "Please choose one programming language:", pady=15 )  
lb.pack()  

c = Radiobutton(mainWindow, text="C", variable=radio, value=0,  
                  command=choice)  
c.pack( anchor = W )  
  
java = Radiobutton(mainWindow, text="Java", variable=radio, value=1,  
                  command=choice)  
java.pack( anchor = W) 

python = Radiobutton(mainWindow, text="Python", variable=radio, value=2,  
                  command=choice)  
python.pack( anchor = W) 
  
message = Label(mainWindow)  
message.pack()   

mainWindow.mainloop() 

Output:

Python Tkinter RadioButton

Various possible options in Python Tkinter RadioButton:

OptionDescription
activebackgroundThis option is used to set background color of widget under focus.
activeforegroundThis option is used to set foreground color of widget under focus.
anchorThis option helps us to set position of text inside the widget. By default value of anchor is CENTER.
bgThis option helps us to set normal background color of widget.
bitmapThis option helps us to display an image. You just have to set this option equal to an image object.
borderwidthThis option is used to set the width of border.
commandThis option helps us to mention a function to every time when RadioButton will change its state.
cursorThis option helps us to set the style of cursor like an arrow, dot etc
fontThis option is used to set font type in widget.
fgThis option helps us to set normal foreground colour of widget.
heightThis option helps us to set height of widget.
highlightcolorThis option is used to set highlight color shown to the widget under focus.
highlightbackgroundThis option is used to set color of the focus highlight when the widget is not having the focus.
imageThis option helps us to show image with radio button instead of text.
justifyThis helps us to automatically organize the text in multiple lines.
padxThis option helps us to set space left and right between text and RadioButton.
padyThis option helps us to set space above and below.
reliefThis helps us to set the style of the border by which is default Flat.
selectcolorThis option used to set color of text when radio button will selected state.
selectimageThis option used to display image when radio button will selected state.
stateThis option is use to set state or RadioButton. By default it is NORMAL. You can also set it DISABLE.
textThis option helps us to set string source for label.
textvariableThis option is use to mention text source(variable.)
underlineThis option helps us to underline specified letter of the text.
valueThis option is used to set value of each RadioButton.
variableThis option is used to set variable in RadioButton.
widthThis helps us to set the width of the widget.
wraplengthThis option is used to wrap text into number of lines.

Python Tkinter RadioButton Methods:

After learning about various available options in Python Tkinter RadioButton. Its time to check out some available methods for Python Tkinter RadioButton widget.

METHODDescription
deselect()This method called to deselect the RadioButton.
flash()This method is used to flash RadioButton between normal and active state.
invoke()This method helps us to invoke the associated method with state of a RadioButton is changed.
select()This method is used to select the RadioButton.
Spread the love