Tkinter Radiobutton

You have learned label, button and Checkbutton in the last sections. Tkinter widgets have quite similar options, properties or methods, therefore from this section on, we could learn at a fast pace.

The Radiobuton is one-of-many type button. There are more than one options in the selection, but the user could only select one of them. The same as introduced widgets before, Radibutton could contain text or image and could bind callback function or method to it. This callback function is trigger automatically when that button is pressed.

Tkinter Radiobutton Basic Example

Tkinter Radiobutton_Basic.py
import tkinter as tk
 
app = tk.Tk() 
app.geometry('150x100')

radioValue = tk.IntVar() 

 
rdioOne = tk.Radiobutton(app, text='January',
                             variable=radioValue, value=1) 
rdioTwo = tk.Radiobutton(app, text='Febuary',
                             variable=radioValue, value=2) 
rdioThree = tk.Radiobutton(app, text='March',
                             variable=radioValue, value=3)

rdioOne.grid(column=0, row=0)
rdioTwo.grid(column=0, row=1)
rdioThree.grid(column=0, row=2)

app.mainloop()

The result of above Radiobutton basic demo is below

Tkinter Checkbutton Basic Example

radioValue = tk.IntVar()

Just as check button, radio buttons need to associate values of particular data type with them.

rdioOne = tk.Radiobutton(app, text='January',
                             variable=radioValue, value=1) 
rdioTwo = tk.Radiobutton(app, text='Febuary',
                             variable=radioValue, value=2) 
rdioThree = tk.Radiobutton(app, text='March',
                             variable=radioValue, value=3)

Here, there buttons with the text of “January”, “February” and “March” are created here. Radio buttons in the same group shall and must have unique values.

Question
What if they have duplicate values?

Change Tkinter Radiobutton Indicator Type

The default radio button indicator is a circular hole with white space inside, but it could also be replaced with a box containing text or image. The text box is raised when the radio button is not selected, and is sunken when it is pressed.

Tkinter Tutorial Radiobutton Raised and Sunken

rdioOne = tk.Radiobutton(app, text='I am raised',
                             variable=radioValue, value=1,
                             indicatoron = 0) 

indicatoron is the option to enable raido button circular hole indictor on or off. indicatoron=indicator on

Bind Tkinter Radiobutton Callback Function

It is the exactly the same with Tkinter Checkbutton in the option how to set the button callback function. Use option command=.