How do I get the value of a Checkbutton in tkinter?
A Checkbutton does not hold its own state in a way you can read. Attach a variable when you build it, and ask the variable — .get() on the checkbutton itself is not how this widget works.
import tkinter as tk
root = tk.Tk()
subscribe = tk.BooleanVar()
tk.Checkbutton(root, text="Email me", variable=subscribe).pack()
def save():
if subscribe.get(): # True or False
print("subscribed")
tk.Button(root, text="Save", command=save).pack()
root.mainloop() IntVar, BooleanVar, or your own values
An IntVar gives you 1 when ticked and 0 when not, which is the traditional pairing and reads fine in an if. A BooleanVar gives you True and False instead, which is usually clearer at the point of use and is worth preferring in new code.
When the value has to be something else entirely — a string to store, or a code to send — onvalue and offvalue set what the variable holds in each state, and the variable type has to match the values you choose.
ticked = tk.IntVar() # 1 / 0
ticked = tk.BooleanVar() # True / False
size = tk.StringVar(value="no")
tk.Checkbutton(root, text="Gift wrap", variable=size,
onvalue="yes", offvalue="no")Why a fresh onvalue/offvalue pair can read as neither
A variable starts at its own default, not at your offvalue. A StringVar with onvalue="yes" and offvalue="no" reads as the empty string until the box is clicked once, so a check for == "no" is false even though the box is plainly unticked. Give the variable the off value when you create it and the two agree from the start.
size = tk.StringVar(value="no") # not just tk.StringVar()Reacting the moment it is ticked
A command runs after the variable has been updated, so it can read the new state immediately. This is the usual way to enable or disable a field that only matters when the box is ticked.
Unlike radio buttons, checkbuttons are independent by default: leave the variable off and tkinter gives each one its own hidden variable, so they do not interfere with each other. That is convenient right up to the moment you want to read one.
def on_toggle():
print("now", subscribe.get())
tk.Checkbutton(root, text="Email me", variable=subscribe, command=on_toggle)Setting and clearing it from code
Write to the variable and the tick follows, which is how you restore saved settings when a window opens. The widget also has .select(), .deselect() and .toggle(), but going through the variable keeps one source of truth.
subscribe.set(True) # tick it
subscribe.set(False) # clear it Doing it without the lookup
Drop a Checkbutton on and the export creates an IntVar for it and wires it up, so the value is readable from the first run. Handlers are stubs on the same class, which is where .get() belongs.
Lay the window out visually and read the generated Python as you go.
Open the tkinter designer →