Tkinter Checkbutton

The Checkbutton widget is a widget holding a value. Checkbuttons could contain either text or images. It could also link the callback function to be called when the checkbutton is clicked.

To create a checkbutton widget in an existing parent window parent, use

tk.Checkbutton(parent, option, ...)

Tkinter Checkbutton Basic Example

Tkinter CheckButton_Basic.py
import tkinter as tk
 
app = tk.Tk() 
app.geometry('150x100')

chkValue = tk.BooleanVar() 
chkValue.set(True)
 
chkExample = tk.Checkbutton(app, text='Check Box', var=chkValue) 
chkExample.grid(column=0, row=0)
 
app.mainloop()

Tkinter Tutorial Checkbutton Basic Example

chkValue = tk.BooleanVar() 

Each checkbutton should be associated with a variable. The data type of variable is determined by the checkbutton when selected/deselected.

chkValue.set(True)

It sets the original value of the checkbutton. As it is just assigned with the Boolean data type, you only have two options here, True or False.

chkExample = tk.Checkbutton(app, text='Check Box', var=chkValue) 

Now the checkbutton instance is eventually created with the variable chkValue we made above.

chkExample.grid(column=0, row=0)

grid is another type of Tkinter layout mangers besides pack introduced in the section Tkinter Label.

Tkinter has three layout/geometry managers,

  • pack
  • grid
  • place

We will introduce these layout/geometry managers in one section of this tutorial.

Tkinter Checkbutton Select/Deselect

User could click checkbutton in GUI to select or deselect the button. You could also select or deselect the checkbutton by using the method select() and deselect().

chkExample.select()
print "The checkbutton value when selected is {}".format(chkValue.get())
chkExample.select()
print "The checkbutton value when deselected is {}".format(chkValue.get())
The checkbutton value when selected is True
The checkbutton value when deselected is False

Here, the value of the checkbutton is obtained by get() method.

Tkinter Checkbutton State Toggle

The state of check button could be modified by select() and deselect(), and it could also be toggled by using the toggle() method.

Tkinter CheckButton_Select_Deselect.py
import tkinter as tk
 
app = tk.Tk() 
app.geometry('150x100')

chkValue = tk.BooleanVar() 
chkValue.set(True)
 
chkExample = tk.Checkbutton(app, text='Check Box', var=chkValue) 
chkExample.grid(column=0, row=0)


print "The checkbutton original value is {}".format(chkValue.get())
chkExample.toggle()
print "The checkbutton value after toggled is {}".format(chkValue.get())
chkExample.toggle()
print "The checkbutton value after toggled twice is {}".format(chkValue.get())

app.mainloop()
The checkbutton original value is True
The checkbutton value after toggled is False
The checkbutton value after toggled twice is True

Tkinter Checkbutton Callback Function Binding

Checkbutton widget is used to select the states and it could also bind the call back function to the event when it is selected/deselected, or more straightforward, toggled. Whenever the check button state is toggled, the call back function is triggered.

Tkinter CheckButton_CallBack.py
import tkinter as tk
from _cffi_backend import callback
 
def callBackFunc():
    print "Oh. I'm clicked"
    
app = tk.Tk() 
app.geometry('150x100')

chkValue = tk.BooleanVar() 
chkValue.set(True)
 
chkExample = tk.Checkbutton(app, text='Check Box', 
                            var=chkValue, command=callBackFunc) 
chkExample.grid(column=0, row=0)

app.mainloop()

Every time when you press the checkbutton, you could see it prints Oh. I'm clicked in the console.

Option command in Checkbutton class is meant to bind the call back function or method when the button is pressed.

Change Tkinter Checkbutton Default Value

The default value corresponding to non-selected checkbutton is 0, and the default value of selected checkbutton is 1. And you could also change checkbutton default values and their associated data type to the other value and/or data type.

Tkinter CheckButton_Select_OnValue_OffValue.py
import tkinter as tk
 
app = tk.Tk() 
app.geometry('150x100')

chkValue = tk.StringVar() 
 
chkExample = tk.Checkbutton(app, text='Check Box', var=chkValue,
                            onvalue="RGB", offvalue="YCbCr") 
chkExample.grid(column=0, row=0)

chkExample.select()
print "The checkbutton value when selected is {}".format(chkValue.get())
chkExample.deselect()
print "The checkbutton value when deselected is {}".format(chkValue.get())

app.mainloop()

The checkbutton value when selected is RGB
The checkbutton value when deselected is YCbCr
chkExample = tk.Checkbutton(app, text='Check Box', var=chkValue,
                            onvalue="RGB", offvalue="YCbCr") 

onvalue and offvalue are options to change values of the selected and non-selected states. They could have data types like Int, String, Float or others.

Attention
The Tkinter data type associated to the check button shall be the same as that of onvalue and offvalue. Otherwise, it reports _tkinter.TclError