Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 40 ยท Widgets Basics

Tkinter Checkbutton and Radiobutton

Offer choices with Tkinter Checkbutton and Radiobutton: onvalue and offvalue, shared variables for radio groups, command callbacks, indicator styles and OptionMenu.

Two widgets let users choose without typing. A Checkbutton is an independent on/off switch (a checkbox). Radiobuttons come in a group where exactly one can be selected. Both are driven by a control variable: the variable is the state, and it is what you read.

Checkbutton

Python
import tkinter as tk

root = tk.Tk()
newsletter = tk.BooleanVar(value=True)

tk.Checkbutton(root, text="Send me the newsletter", variable=newsletter).pack(anchor="w", padx=20, pady=20)
tk.Button(root, text="Show", command=lambda: print(newsletter.get())).pack()
root.mainloop()

By default a checkbutton stores 1 when checked and 0 when not, so an IntVar is its natural partner, though BooleanVar reads more clearly. To store other values, set onvalue and offvalue, and use a StringVar:

Python
plan = tk.StringVar(value="free")
tk.Checkbutton(root, text="Pro plan", variable=plan,
               onvalue="pro", offvalue="free").pack()

When the variable holds neither value (for example, None or something unexpected), the checkbutton displays in a third, "tristate" look. You can use that deliberately: tristatevalue="" shows mixed state, such as when "select all" covers a partly selected list.

Callbacks and options

A command runs when the user toggles the button, after the variable has already changed. select(), deselect(), toggle() and invoke() change the state from code (invoke also runs the command).

Python
def on_toggle():
    state = "on" if dark.get() else "off"
    status.config(text=f"Dark mode {state}")

dark = tk.BooleanVar()
tk.Checkbutton(root, text="Dark mode", variable=dark, command=on_toggle).pack()
status = tk.Label(root, text="Dark mode off")
status.pack()

Other options: indicatoron=False draws the widget like a push button that stays pressed, selectcolor colors the box, anchor, padx, font and state work as for buttons. On some platforms fg/bg do not color the checkmark.

Many checkbuttons, many variables

Each independent choice needs its own variable. Build them from data in a dictionary so you can read all of them later:

Python
toppings = ["Cheese", "Mushrooms", "Olives", "Peppers"]
chosen = {name: tk.BooleanVar() for name in toppings}

for name, var in chosen.items():
    tk.Checkbutton(root, text=name, variable=var).pack(anchor="w")

def order():
    picks = [name for name, var in chosen.items() if var.get()]
    print("Pizza with:", ", ".join(picks) or "nothing")

Radiobutton

Radiobuttons in a group share one variable, and each has its own value. Selecting one sets the variable to its value, and that automatically deselects the others.

Python
import tkinter as tk

root = tk.Tk()
size = tk.StringVar(value="medium")

for label, value in [("Small", "small"), ("Medium", "medium"), ("Large", "large")]:
    tk.Radiobutton(root, text=label, variable=size, value=value).pack(anchor="w", padx=20)

tk.Button(root, text="Order", command=lambda: print(size.get())).pack(pady=10)
root.mainloop()

The sharing is the whole mechanism: there is no "group" object. Two radiobuttons that use the same variable are in the same group; a different variable starts a different group. This explains two classic bugs:

  • Nothing is selected, or all look selected. The variable's starting value matches no button, or you gave every button the same value. Set the variable to one of the values first.
  • Two groups interfere. Both groups reused the same variable by accident, so clicking in one deselects the other.

An IntVar with value=1, 2, 3 also works and is common. If the variable starts at 0 and no button has value=0, nothing is selected, which is a legitimate "no choice yet" state.

The model below shows the shared-variable idea in plain Python, including what happens when two groups collide.

Python Stand-in: radio groups are shared variables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Radiobutton options

Like checkbuttons: command, indicatoron=False (makes a row of toggle buttons, a nice segmented control), selectcolor, font, state, anchor. radio.invoke() selects it and runs the command; radio.select() and radio.deselect() do not run it.

Python
for label, value in [("Left", "left"), ("Center", "center"), ("Right", "right")]:
    tk.Radiobutton(toolbar, text=label, variable=align, value=value, indicatoron=False,
                   width=8, command=apply_alignment).pack(side="left")

Reading state in practice

Use a Checkbutton for "is this option on?" and Radiobuttons for "which one of these?" For more than about five exclusive options, a drop-down is more compact. Tkinter's classic drop-down is OptionMenu:

Python
color = tk.StringVar(value="red")
tk.OptionMenu(root, color, "red", "green", "blue").pack()
color.trace_add("write", lambda *a: print("picked", color.get()))

For a nicer drop-down with typing support, use ttk.Combobox (chapter 7).

A complete form

This stand-in computes a pizza price from a checkbutton dictionary and a radio value, the same data a real form's variables would hold. The calculation is separate from the widgets, so it is easy to test.

Python Stand-in: price from variables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do two Radiobuttons end up in the same group?

    Tkinter has no group object. The shared control variable is what makes selecting one deselect the others.
  2. 2. A radio group shows no selection at startup. What is the usual reason?

    A radiobutton looks selected only if variable.get() equals its value. Set the variable to one of them first.
  3. 3. How can a Checkbutton store the strings 'yes' and 'no'?

    onvalue and offvalue define what the variable holds when checked or unchecked.
  4. 4. You build 5 checkbuttons that should be independent. How many variables do you need?

    Each independent on/off choice needs its own variable; a shared variable would make them toggle together.

Summary

  • A Checkbutton toggles its variable between onvalue and offvalue (1 and 0 by default); each independent choice needs its own variable.
  • Radiobuttons share one variable and each has a unique value; the variable is the group.
  • Start the shared variable at one of the values, or at a non-matching value for "nothing chosen".
  • command runs after the variable changes; invoke() clicks from code; indicatoron=False gives toggle-button looks.
  • OptionMenu and the ttk Combobox are compact alternatives to long radio groups.

Next: Listbox and Scrollbar