Core Concept
Both Checkbutton and Radiobutton widgets rely on Tkinter control variables (such as tk.IntVar or tk.StringVar) to track they state. Understanding how these variables interact with the widgets is key to using them effectively.
Multi-Selection (Checkbutton)
Checkbuttons allow the user to select multiple independent options. Each Checkbutton is associated with its own control variable.
import tkinter as tk
root = tk.Tk()
root.geometry('500x300')
var1 = tk.IntVar()
var2 = tk.IntVar()
def on_click():
print("Checkbutton toggled")
cb1 = tk.Checkbutton(root, text='Option 1', variable=var1, onvalue=1,
offvalue=0, width=10, height=5, command=on_click)
cb1.pack()
cb2 = tk.Checkbutton(root, text='Option 2', variable=var2, onvalue=1,
offvalue=0, width=10)
cb2.pack()
def confirm():
if var1.get() == 1 and var2.get() == 1:
print("Both selected")
elif var1.get() == 1 or var2.get() == 1:
print("One selected")
else:
print("None selected")
btn = tk.Button(text='Confirm', command=confirm, width=10, height=2)
btn.pack()
root.mainloop()
Key parameters:
variable: A Tkinteer variable (e.g.,tk.IntVar) that holds the state.onvalue: The value assigned to the variable when the checkbox is checked.offvalue: The value assigned when unchecked.command: Callback funciton triggered on state change.
Single Selection (Radiobutton)
Radiobuttons enforce mutual exclusivity. All radiobuttons in the same group share the same control variable; each has a unique value.
group_var = tk.StringVar()
group_var.set('55') # Pre-selects the first option
rb1 = tk.Radiobutton(root, variable=group_var, text='Option 3', value='55')
rb1.pack()
rb2 = tk.Radiobutton(root, variable=group_var, text='Option 4', value='100')
rb2.pack()
def show_selection():
if group_var.get() == '55':
print("Option 3 chosen")
elif group_var.get() == '100':
print("Option 4 chosen")
confirm2 = tk.Button(text='Confirm', command=show_selection, width=10, height=2)
confirm2.pack()
Important notes:
- Use
set()on the shared variable to define the default selection. - Without a default, Tkinter may auto-select the first radiobutton, but explicit initialization is safer.
- The
valueparameter assigns what the variable becomes when that radiobutton is selected.
Complete Example
import tkinter as tk
root = tk.Tk()
root.geometry('500x300')
# Checkbuttons
chk_var1 = tk.IntVar()
chk_var2 = tk.IntVar()
def cb_toggled():
print("Checkbutton changed")
ck1 = tk.Checkbutton(root, text='Check 1', variable=chk_var1, onvalue=1, offvalue=0, command=cb_toggled)
ck1.pack()
ck2 = tk.Checkbutton(root, text='Check 2', variable=chk_var2, onvalue=1, offvalue=0)
ck2.pack()
def check_confirm():
if chk_var1.get() and chk_var2.get():
print("Both checked")
elif chk_var1.get() or chk_var2.get():
print("One checked")
else:
print("None checked")
btn_check = tk.Button(text='Check Confirm', command=check_confirm)
btn_check.pack()
# Radiobuttons
rad_var = tk.StringVar()
rad_var.set('A') # default selection
rb1 = tk.Radiobutton(root, variable=rad_var, text='Option A', value='A')
rb1.pack()
rb2 = tk.Radiobutton(root, variable=rad_var, text='Option B', value='B')
rb2.pack()
def rad_confirm():
if rad_var.get() == 'A':
print("Selected A")
elif rad_var.get() == 'B':
print("Selected B")
btn_rad = tk.Button(text='Radio Confirm', command=rad_confirm)
btn_rad.pack()
root.mainloop()