Tkinter Checkbutton

In this tutorial, I will teach you about the Tkinter checkbutton widget in python. I will also be discussing various options that we can use to customize it and some code examples for better understanding. So let’s get started.

The checkbutton widget in Tkinter allows us to take selection based inputs from the user.it can have two states(on and off) and it can display any text or image.

We can also link any function or method to the checkbutton mouse click event. means, when the user will change the state of the checkbutton that function will be called.

Tkinter checkbutton is different from the radiobutton where the user can only select one option.

Learn more about the checkboxes here.

Let’s look at the syntax of checkbutton.

Syntax:

c = CheckButton(window,**options)

Options :

Let’s look at the various options that we can use to customize the checkbuttons.

activebackgroundSet the background color of the check button when it’s active.
activeforegroundSet the text color of the check button when it’s active.
anchorSet the location of the text of checkbutton. default is CENTER.
backgroundSet the background color of the checkbutton.
borderwidthSet the border width of the checkbutton.
commandSet the function to be called when the checkbutton is toggled.
compoundUse this option to control the position of image and text when combined.
cursorSet the cursor to show when the mouse pointer is on checkbutton
disabledforegroundSet the background color of checkbutton when it’s in the disabled state.
fontSet the font for the text of checkbutton.
foregroundSet the text color of checkbutton.
heightSet the height of the checkbutton widget.
imageSet the image to display.
offvalueSet the value of the button when it’s off. default is 0.
onvalueSet the value of the button when it’s on. default is 1.
padxSet the horizontal padding around checkbutton.
padySet the vertical padding around checkbutton.
selectcolorSet the color of the selector.
selectimageSet the image for the selector.
stateSet the state of the checkbutton. use NORMAL(default), ACTIVE, DISABLED.
textSet the text to display for checkbutton.
textvariableUse the textvariable to control the text of the check buttons.
underlineSet which character to underline.
widthSet the width of the checkbutton.

Tkinter Checkbutton Examples :

For a better understanding of the concepts. let’s look at some code examples.

Example 1 : Simple Checkbutton

In this code example, we will display the simple checkbutton and button on the window. on button click, it will print the current state of checkbutton.

from tkinter import *

window = Tk()

# Variable to tract the value of checkbutton
value = IntVar()

def btnclick():
    # Function to call on button click
    print("CheckBox Value : ",value.get())

# Create a check button with text "Check It"
# and track its value using variable option.
Checkbutton(window,text = "Check It",variable = value).pack()

Button(window,text = "Get Value",command = btnclick).pack()

window.mainloop()

Output:

tkinter checkbutton example 1 output

Example 2 : Images In Tkinter Checkbutton

In this code example, we will display the image in place of text with checkbutton.

from tkinter import *

window = Tk()

image = PhotoImage(file="circle.png") # loqe the png image

# Set the image to the checkbutton
Checkbutton(window,image = image).pack()

# Set both text and image
Checkbutton(window,text = "Text + Image",image = image,compound = LEFT).pack()

window.mainloop()

Output :

tkinter checkbox example 2 output

Example 3 : Styled Checkbutton

In this code example, we will add some basic styling to the checkbuttons.

from tkinter import *

window = Tk()

# ADD some style to checkbutton
Checkbutton(window,text = "White In Black",
    background = "black",foreground = "white",
    font = "arial 15 bold",padx = 20).pack()

window.mainloop()

Output :

tkinter checkbutton code example 3 output

Example 4 : Add Command To Checkbutton

In this code example, we will create a checkbutton and associate the function with it.

from tkinter import *

window = Tk()

def onclick():
    # Function to call when user clicks on button
    print("Checkbox is clicked!")
    print("value : ",value.get())

value = IntVar()
Checkbutton(window,text = "Check It",variable = value,command = onclick).pack()

window.mainloop()

Output :

tkinter checkbutton code example 4 output

Example 5 : Control The State With Button

In this final code example, we will control the state of checkbutton with buttons.

from tkinter import *

window = Tk()

c = Checkbutton(window,text = "Check It")
c.pack()

def toggle():
    print("Toogle the checkbutton")
    c.toggle()

def select():
    print("select the checkbutton")
    c.select()

def deselect():
    print("deselect the checkbutton")
    c.deselect()

Button(window,text = "Toggle",command = toggle).pack()
Button(window,text = "Select",command = select).pack()
Button(window,text = "Deselect",command = deselect).pack()

window.mainloop()

Output :

code example 5 output

That’s wrap for this tutorial.

Leave a Reply