Tkinter Tutorial - Radiobutton

Jinku Hu Jan 30, 2023
  1. Tkinter Radiobutton Basic Example
  2. Tkinter Radiobutton - Get Value
  3. Change Tkinter Radiobutton Indicator Type
  4. Bind Tkinter Radiobutton Callback Function
Tkinter Tutorial - 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 a one-of-many type button. It has 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

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?

Tkinter Radiobutton - Get Value

The radio buttons in the same group share the same variable, radioValue as shown in the above example, and are assigned with different values with the option value.

The value of the selected radio button automatically updates the variable radioValue that is a tk.IntVar.

The label text automatically reflects the value of the selected button in the below example codes.

import tkinter as tk

app = tk.Tk()
app.geometry("200x100")

radioValue = tk.IntVar()


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


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


labelValue = tk.Label(app, textvariable=radioValue)
labelValue.grid(column=2, row=0, sticky="E", padx=40)


app.mainloop()

The textvariable of the labelValue label is the same with variable option of the radio buttons, so that the label text could automatically updates itself.

Tkinter Radiobutton_get value

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 exactly the same with [Tkinter Checkbutton](/tutorial/tkinter-tutorial/tkinter-checkbutton/#Tkinter Checkbutton Callback Function Binding) in the option how to set the button callback function. Use option command=.

Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook