Python Tkinter Checkbutton – How to use

In this Python tutorial, we will discuss about Python tkinter checkbutton.

  • Tkinter Checkbutton python
  • Tkinter Checkbutton get value
  • Tkinter Checkbutton image placement
  • Tkinter Checkbutton command
  • Tkinter Checkbutton default checked
  • Tkinter Checkbutton value
  • Tkinter Checkbutton size
  • Tkinter Checkbutton set value
  • Tkinter Checkbutton deselect
  • Tkinter Checkbutton deselect
  • Tkinter Checkbutton grid
  • Tkinter Checkbox example

Python Tkinter Checkbutton

New to Python GUI Programming?, first check Python GUI Programming (Python Tkinter) and How to read a text file using Python Tkinter.

  • The checkbutton is a square box with a tick mark when clicked.
  • Checkmark disappears on every even click if it was not checked already.
  • Checkbuttons are similar to radiobuttons but it allows multiple selections.
  • Checkbuttons are also called Checkboxes.
  • Here is an example of Checkbutton
python tkinter checkbutton firstlook
Python Tkinter Checkbutton

Tkinter Checkbutton get value

  • Get value is used to pull the value out of the checkbutton that was assigned while creating checkbutton
  • This value can be Integer, String, or Booleans.
  • IntVar() is used to get integer values
  • StringVar() is used to get string values
  • BooleanVar() is used to get boolean values i.e True or False.
  • Value can be used with if-else to run any action that you want to happen when checkbutton is checked or unchecked

Code:

In this code, we have implemented get value. Here value assigned to onvalue & offvalue is an integer that is why IntVar() is used as a variable.

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('200x80')

def isChecked():
    if cb.get() == 1:
        btn['state'] = NORMAL
        btn.configure(text='Awake!')
    elif cb.get() == 0:
        btn['state'] = DISABLED
        btn.configure(text='Sleeping!')
    else:
        messagebox.showerror('PythonGuides', 'Something went wrong!')

cb = IntVar()

Checkbutton(ws, text="accept T&C", variable=cb, onvalue=1, offvalue=0, command=isChecked).pack()
btn = Button(ws, text='Sleeping!', state=DISABLED, padx=20, pady=5)
btn.pack()

ws.mainloop()

Output:

In this output, get is used to compare the value and then change the button state & text accordingly. As you can see if the checkbox is checked then button is Normal & have text ‘Awake‘ whereas when checkbutton is unchecked then button state is disabled & text says ‘Sleeping!’

python tkinter checkbutton get
Tkinter Checkbutton get value

Python Tkinter Checkbutton image placement ( indicatoron )

  • Image placement can be done by setting indicatoron to False.
  • indicatoron is responsible for creating a box with check mark
  • setting indicatoron to false removes the default indicator.
from tkinter import *
ws = Tk()
ws.title('PythonGudes')
ws.geometry('200x100')

def switchState():
    if cb1.get() == 1:
       disp_Lb.config(text='ON')
        
    elif cb1.get() == 0:
        disp_Lb.config(text='OFF')
    else:
        disp_Lb.config(text='error!')

switch_on = PhotoImage(width=50, height=50)
switch_off = PhotoImage(width=50, height=50)

switch_on.put(("green",), to=(0, 0, 23,23))
switch_off.put(("red",), to=(24, 0, 47, 23))
cb1 = IntVar()
Checkbutton(ws, image=switch_off, selectimage=switch_on, onvalue=1, offvalue=0, variable=cb1, indicatoron=False, command=switchState).pack(padx=20, pady=10)
disp_Lb = Label(ws)
disp_Lb.pack()
ws.mainloop()

Output:

In this output, we have displayed a Checkbutton that can be used as a switch. This switch has 2 colours ‘red’ and ‘green’. on each click, it switches between these colours. Also, there is a message box that displays On for green colour and Off for red colour.

Python Tkinter Checkbutton image
Python Tkinter Checkbutton image

Tkinter Checkbutton command

  • The command is used to pass any function or method.
  • Command holds instruction for an action that will be implemented when checkbutton is selected or deselected.
  • keyword command is used followed by the function name.

Code:

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('200x250')

def isChecked():
    return Label(ws, text=f'Checkbutton is checked: , {cb.get()}').pack()
    

cb = BooleanVar()
checkme = Checkbutton(ws, text='Check Me',variable = cb, onvalue=True, offvalue=False, command=isChecked)
checkme.pack()


ws.mainloop()

Output:

In this output, function is created to display the button state in boolean form i.e True or False. This function is passed as a command in the checkbox. Now, every time if checkbutton is checked then it prints True otherwise prints False.

python tkinter checkbutton command
Python Tkinter Checkbutton get value

Tkinter Checkbutton default checked

  • This means the cehck box is already checked.
  • to do so all we need to do is provide value in the variable
  • Example: cb = IntVar(value=1)
python tkinter checkbutton default
Tkinter Checkbutton default checked

Python Tkinter Checkbutton value

  • Value plays important role in both Checkbutton and Radiobutton
  • type of value is decided by variable.
  • if the variable is IntVar() that means the value will be an integer
  • if the variable is StringVar() which means the value will be String.
  • if the variable is BooleanVar() that means the value will be True or False
  • In checkbutton there are 2 types of values: onvalue & offvalue.
  • onvalue: return the provided value when box is checked.
  • offvalue: returns the provided value when box is unchecked.
  • generally onvalue=1 and offvalue=0

Syntax:

cb = IntVar()

Checkbutton(ws, text="any text", variable=cb, onvalue=value, offvalue=value, command=function/method).pack()

Code:

In this example we are taking onvalue as 5 and offvalue as 6 for demonstration purpose.

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('200x80')

def isChecked():
    if cb.get() == 5:
        btn['state'] = NORMAL
        btn.configure(text='Awake!')
    elif cb.get() == 6:
        btn['state'] = DISABLED
        btn.configure(text='Sleeping!')
    else:
        messagebox.showerror('PythonGuides', 'Something went wrong!')

cb = IntVar(value=1)

Checkbutton(ws, text="accept T&C", variable=cb, onvalue=5, offvalue=6, command=isChecked).pack()
btn = Button(ws, text='Sleeping!', state=DISABLED, padx=20, pady=5)
btn.pack()

ws.mainloop()

Output:

There is no difference in the output, as this is the same program as in previous section. But in this we have assigned onvalue as 5 and offvalue as 6

python tkinter checkbutton value
Python Tkinter Checkbutton value

Tkinter Checkbutton size

  • This is not possible to change the size of the Checkbutton.
  • Height & width will change the position of the Checkbutton.
  • Images can be placed with the required size.
  • to know more >> check our Tkinter Checkbutton image placement section.

Tkinter Checkbutton set value

  • Set value is used to set the default value of the checkbutton.
  • setting a value will depend upon the type of the variable used.
  • In case the variable is an integer then the argument passed in a set must be an integer.
  • to understand better we will use the example in Checkbutton value section
  • In that example checkbutton for terms & condition is unchecked
  • but we will set it to check. So every time the program will be executed, the box will be checked already.

Code:

In this code, we have set the value of checkbutton to 1 that means it is checked.

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('200x80')

def isChecked():
    if cb.get() == 1:
        btn['state'] = NORMAL
        btn.configure(text='Awake!')
    elif cb.get() == 0:
        btn['state'] = DISABLED
        btn.configure(text='Sleeping!')
    else:
        messagebox.showerror('PythonGuides', 'Something went wrong!')

cb = IntVar()
cb.set(1)
Checkbutton(ws, text="accept T&C", variable=cb, onvalue=1, offvalue=0, command=isChecked).pack()
btn = Button(ws, text='Sleeping!', state=DISABLED, padx=20, pady=5)
btn.pack()

ws.mainloop()

Output:

In this output, the checkbutton is already checked.

python tkinter checkbutton set

Tkinter Checkbutton deselect

  • deselect is used to remove the check mark from the checkbutton.
  • If user want to clear the selection, in that case deselect is used
  • deselect is a function & requires no argument.

Code:

In this code, multiple checkbuttons are created and these checkbuttons are set to checked by default. There is a buttons that can deselect all the checkbuttons.

from tkinter import *
 
ws = Tk() 
ws.geometry('300x250')
ws.title('Python Guides')

def clear_selection():
    cb1.deselect()
    cb2.deselect()
    cb3.deselect()
    cb4.deselect()
    cb5.deselect()
    cb6.deselect()
    
var = BooleanVar() 
var.set(True)
 
cb1 = Checkbutton(ws, text='Click me!', variable=var)
cb1.pack()
cb2 = Checkbutton(ws, text='Click me!', variable=var)
cb2.pack()
cb3 = Checkbutton(ws, text='Click me!', variable=var)
cb3.pack()
cb4 = Checkbutton(ws, text='Click me!', variable=var)
cb4.pack()
cb5 = Checkbutton(ws, text='Click me!', variable=var)
cb5.pack()
cb6 = Checkbutton(ws, text='Click me!', variable=var)
cb6.pack()

Button(ws, text='Deselect All Check buttons', command=clear_selection).pack()



 
ws.mainloop()

Output:

In these outputs, first image shows the execution of the code. Whereas second images shows the deselection of checkbuttons when clicked. Multiple checkbuttons can be deselected using function deselect.

python tkinter checkbutton deselect
python tkinter checkbutton when clicked

Tkinter Checkbutton grid

  • Grid is a geometry manager.
  • It positions widget in a row & column format

Code:

In this code, checkbuttons are created using grid. Name provided to checkbutton refers to their location in row and column format.

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('300x250')
ws.config(bg="grey")



Checkbutton(ws, text='0, 0').grid(row=0, column=0)
Checkbutton(ws, text='0, 1').grid(row=0, column=1)
Checkbutton(ws, text='0, 2').grid(row=0, column=1)
Checkbutton(ws, text='1, 0').grid(row=1, column=0)
Checkbutton(ws, text='1, 1').grid(row=1, column=1)
Checkbutton(ws, text='1, 2').grid(row=1, column=2)
Checkbutton(ws, text='2, 0').grid(row=2, column=0)
Checkbutton(ws, text='2, 1').grid(row=2, column=1)
Checkbutton(ws, text='2, 2').grid(row=2, column=2)

ws.mainloop()

Output:

In this output, Checkbutton with name 1,2 tells that it is situated at row first of second column. Similarly all the checkbuttons have name as per their position.

python tkinter checkbutton grid

Python Tkinter Checkbutton example

Let us check a Python Tkinter Checkbutton example.

As an example, we are doing a mini project. We are creating a simple form that will accept user details & user won’t be able to click on submit button until he checks the terms & condition.

Code:

from tkinter import *
from tkinter import messagebox

ws =Tk()
ws.title('PythonGuides')
ws.geometry('250x300')
ws.configure(bg='#dddddd')

def selection():
    choice = var.get()
    if choice == 1:
        m = 'Ms.'
    elif choice == 2:
        m = 'Mr.'
    elif choice == 3:
        pass
    return m

def submit():
    try:
        name = name_Tf.get()
        m = selection()
        return messagebox.showinfo('PythonGuides', f'{m} {name}, submitted form.')
    except Exception as ep:
        return messagebox.showwarning('PythonGuides', 'Please provide valid input')

def termsCheck():
    if cb.get() == 1:
        submit_btn['state'] = NORMAL
    else:
        submit_btn['state'] = DISABLED
        messagebox.showerror('PythonGuides', 'Accept the terms & conditions')


frame1 = Label(ws, bg='#dddddd')
frame1.pack()
frame2 = LabelFrame(frame1, text='Gender', padx=30, pady=10)

var =IntVar()
cb = IntVar()

Label(frame1, text='Full Name').grid(row=0, column=0, padx=5, pady=5)
Label(frame1, text='Email').grid(row=1, column=0, padx=5, pady=5)
Label(frame1, text='Password').grid(row=2, column=0, padx=5, pady=5)
Radiobutton(frame2, text='Female', variable=var, value=1,command=selection).pack()
Radiobutton(frame2, text='Male', variable=var, value=2,command=selection).pack(anchor=W)
Radiobutton(frame2, text='Others', variable=var, value=3,command=selection).pack()
name_Tf = Entry(frame1)
name_Tf.grid(row=0, column=2)
Entry(frame1).grid(row=1, column=2)
Entry(frame1, show="*").grid(row=2, column=2)
frame2.grid(row=3, columnspan=3,padx=30)
Checkbutton(frame1, text='Accept the terms & conditions', variable=cb, onvalue=1, offvalue=0,command=termsCheck).grid(row=4, columnspan=4, pady=5)
submit_btn = Button(frame1, text="Submit", command=submit, padx=50, pady=5, state=DISABLED)
submit_btn.grid(row=5, columnspan=4, pady=2)

ws.mainloop()

Output:

  • In this output, a simple form is created using python Tkinter.
  • Widgets like label, entry, radiobutton, checkbutton , frame, labelframe, and button are used.
  • The button will remain in a disabled state until the checkbutton is checked.
  • The program accepts user details and prompts the title, name with submitting a message.
python tkinter checkbutton example
Tkinter Checkbutton example

Here is the another output snap of this program. In this case the button is in Normal state.

python tkinter checkbutton example output
Tkinter Checkbutton example

You may like the following python tutorials:

In this tutorial, we have learned about the Python Tkinter checkbutton. Also, we have covered these topics.

  • Tkinter Checkbutton python
  • Tkinter Checkbutton get value
  • Tkinter Checkbutton image placement
  • Tkinter Checkbutton command
  • Tkinter Checkbutton default checked
  • Tkinter Checkbutton value
  • Tkinter Checkbutton size
  • Tkinter Checkbutton set value
  • Tkinter Checkbutton deselect
  • Tkinter Checkbutton grid
  • Tkinter Checkbox example