Summary: In this tutorial, you’ll learn how to use the Tkinter BooleanVar
class to track the boolean state of a widget.
Introduction to Tkinter BooleanVar #
In Tkinter, the BooleanVar
class is a subclass of the Variable
class. It is a variable class that holds a boolean value.
Like StringVar
, IntVar
, and DoubleVar
, you can use the BooleanVar
object to track the boolean state of a widget such as a RadioButton
or CheckButton
.
When you link a BooleanVar
object with a widget, you can easily track and change widget values from your Python code.
Linking means establishing a two-way binding between a BooleanVar
object and the widget’s state. This results in automatic value synchronization between the two.
In Tkinter, you can link a BooleanVar
object with the following widgets:
- CheckButton
- RadioButton
- Menu CheckButton
Creating a BooleanVar object #
To create a BooleanVar
object, you follow these steps:
First, import Tkinter:
import tkinter as tk
Code language: JavaScript (javascript)
Second, create an instance of the BooleanVar
class:
boolean_var = tk.BooleanVar()
A BooleanVar
object defaults to False
. If you want to set its value to True
, you can use the following:
boolean_var = tk.BooleanVar(value=True)
Code language: PHP (php)
Note that you have to use the value
keyword argument because the first parameter of the BooleanVar
constructor is a master
widget.
Linking a BooleanVar object with a widget #
Once having a BooleanVar
object, you can link it with a widget. For example, you can link a BooleanVar
object with a CheckButton
as follows:
check_button = ttk.Checkbutton(variable=boolean_var)
Note that you can link one BooleanVar object with one or more widgets.
Manipulating widget’s values #
After linking a BooleanVar
object with a widget, you can track and change the widget’s value via the BooleanVar
object.
For example, you can access the current value of the CheckButton
using the get()
method of the BooleanVar
object:
boolean_var.get()
Code language: CSS (css)
You can also check the CheckButton
by setting the value of the BooleanVar
object to True
:
boolean_var.set(True)
Code language: CSS (css)
Tkinter BooleanVar example #
The following program illustrates how to use a BooleanVar
to manage the state of a CheckButton
widget:
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo
root = tk.Tk()
root.title("BooleanVar Example")
root.geometry("300x80")
check_var = tk.BooleanVar(value=True)
check_button = ttk.Checkbutton(
root,
text="Check me",
variable=check_var,
command=lambda: showinfo(
title="Checkbutton State",
message= "Checked" if check_var.get() else "Unchecked"
)
)
check_button.pack()
root.mainloop()
Code language: JavaScript (javascript)

How the program works
First, create a BooleanVar
with the default value True
:
check_var = tk.BooleanVar(value=True)
Code language: PHP (php)
Second, create a CheckButton
widget and link it with the check_var
button:
check_button = ttk.Checkbutton(
root,
text="Check me",
variable=check_var,
command=lambda: showinfo(
title="Checkbutton State",
message="Checked" if check_var.get() else "Unchecked"
)
)
Code language: JavaScript (javascript)
If you check or uncheck, the CheckButton
will execute a lambda to show the current value retrieved from the check_var
variable.
Summary #
- Use a
BooleanVar
object to track and control the boolean state of a widget.