Tkinter Tutorial - Checkbutton

  1. Tkinter Checkbutton Basic Example
  2. Tkinter Checkbutton Select/Deselect
  3. Tkinter Checkbutton State Toggle
  4. Tkinter Checkbutton Callback Function Binding
  5. Change Tkinter Checkbutton Default Value

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, we can 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 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 managers 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

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

chkExample.select()
print("The checkbutton value when selected is {}".format(chkValue.get()))
chkExample.deselect()
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 the get() method.

Tkinter Checkbutton State Toggle

The state of the checkbutton could be modified by select() and deselect(), and 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 callback 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('250x200')

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()

Tkinter Tutorial_CheckButton Callback Function Binding

Whenever 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 the non-selected checkbutton is 0, and the default value of the 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('250x200')

chkValue = tk.StringVar() 
 
chkExample = tk.Checkbutton(app,
                            text='Check Box', 
                            bg = 'gray',
                            fg = 'blue',
                            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()

Tkinter Tutorial_CheckButton Change Properties

chkExample = tk.Checkbutton(app,
                            text='Check Box', 
                            bg = 'gray',
                            fg = 'blue',
                            var=chkValue,
                            onvalue="RGB", 
                            offvalue="YCbCr") 

The bg sets the background color of the checkbutton, including the text area.

The fg sets the foreground color of the checkbutton text.

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

Attention

The Tkinter data type associated with the check button shall be the same as onvalue and offvalue. Otherwise, it raises a _tkinter.TclError

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.