Checkbox widgets are something so obvious you never think about them. They’re like on/off switches and you can have multiple of them. It is one of the widgets included in tkinter.

If you want zero or more options to be clickable, you can use a checkbox. Otherwise you’d use a radiobutton or another type of button.

Related course: Python Desktop Apps with Tkinter

checkbox

tkinter checkbox

The tkinter checkbox widget is a very basic switch. A checkbox in tkinter is named a CheckButton. You can add two checkboxes like this:

1
2
3
4
c1 = tk.Checkbutton(window, text='Python',variable=var1, onvalue=1, offvalue=0, command=print_selection)
c1.pack()
c2 = tk.Checkbutton(window, text='C++',variable=var2, onvalue=1, offvalue=0, command=print_selection)
c2.pack()

demo

The program below adds several checkbuttons to the window. If you click on the checkbox the text above changes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tkinter as tk


window = tk.Tk()
window.title('My Window')
window.geometry('100x100')

l = tk.Label(window, bg='white', width=20, text='empty')
l.pack()

def print_selection():
if (var1.get() == 1) & (var2.get() == 0):
l.config(text='I love Python ')
elif (var1.get() == 0) & (var2.get() == 1):
l.config(text='I love C++')
elif (var1.get() == 0) & (var2.get() == 0):
l.config(text='I do not anything')
else:
l.config(text='I love both')

var1 = tk.IntVar()
var2 = tk.IntVar()
c1 = tk.Checkbutton(window, text='Python',variable=var1, onvalue=1, offvalue=0, command=print_selection)
c1.pack()
c2 = tk.Checkbutton(window, text='C++',variable=var2, onvalue=1, offvalue=0, command=print_selection)
c2.pack()

window.mainloop()

tkinter checkbox

Download Tkinter examples