Tkinter Tutorial - Radiobutton
- Tkinter Radiobutton Basic Example
- Tkinter Radiobutton - Get Value
- Change Tkinter Radiobutton Indicator Type
- Bind Tkinter Radiobutton Callback Function
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
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.
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.
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.
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=
.
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