Python 3 – Tkinter Checkbutton
Tkinter is the standard GUI (Graphical User Interface) package for Python. It is a popular and widely used library for creating graphical interfaces. Checkbutton is one of the important widgets in Tkinter. It provides a checkbox for selecting or deselecting an option. In this article, we will learn how to use the Checkbutton widget in Tkinter with some examples.
Creating Checkbuttons
To create a Checkbutton widget in Tkinter, we need to use the Checkbutton()
constructor. Below is the syntax for creating a Checkbutton:
Checkbutton(parent, options)
Here, parent
is the parent widget on which the Checkbutton will be placed. The options
parameter is optional and can be used to set the various configuration options for the Checkbutton. We can use a variety of options to configure the appearance and behavior of the Checkbutton.
Let’s create a Checkbutton without any options and display it on the screen. Here is the code:
import tkinter as tk
root = tk.Tk()
# create checkbutton
c = tk.Checkbutton(root, text="Check me!")
c.pack()
root.mainloop()
In the code above, we first import the Tkinter module and create a new Tkinter instance named root
. We then create a new Checkbutton widget with the text “Check me!” and add it to the root
window using the pack()
method. Finally, we call the mainloop()
method on root
to start the event loop.
When we run the above code, we will see a Checkbutton with the text “Check me!” on the screen.
Configuring Checkbuttons
We can configure the Checkbutton widget using the various options available for it. Some of the common configuration options are:
text
: sets the text to be displayed next to the checkbox.variable
: sets the variable associated with the Checkbutton. If the Checkbutton is checked, the variable is set to 1, otherwise 0.onvalue
: sets the value to be associated with the variable when the Checkbutton is checked. Default is 1.offvalue
: sets the value to be associated with the variable when the Checkbutton is unchecked. Default is 0.command
: sets a callback function to be called when the Checkbutton is checked or unchecked.state
: sets the state of the Checkbutton. Possible values are NORMAL, ACTIVE, and DISABLED.anchor
: sets the position of the text relative to the checkbox. Possible values are W (west), E (east), N (north), S (south), NW (northwest), NE (northeast), SW (southwest), and SE (southeast).font
: sets the font for the text.
Let’s look at some examples of configuring Checkbuttons.
Example 1 – Setting the variable and onvalue options
In this example, we will set the variable
and onvalue
options for the Checkbutton. We will also associate a label with the Checkbutton to display the value of the variable. Here is the code:
import tkinter as tk
root = tk.Tk()
# create variable
var = tk.StringVar()
# create checkbutton
c = tk.Checkbutton(root, text="Check me!", variable=var, onvalue="checked")
c.pack()
# create label
lbl = tk.Label(root, textvariable=var)
lbl.pack()
root.mainloop()
In the code above, we first create a StringVar
variable named var
. We then create a Checkbutton with the text “Check me!” and associate the var
variable with it using the variable
option. We also set the onvalue
option to “checked” so that when the Checkbutton is checked, the value of var
is set to “checked”. We then create a Label
widget and associate the var
variable with it using the textvariable
option. We add this label to the root
window using the pack()
method.
When we run the above code, we will see a Checkbutton with the text “Check me!” and a label next to it. When we check the Checkbutton, the label will display “checked”, and when we uncheck it, the label will be empty.
Example 2 – Using the command option
In this example, we will use the command
option to bind a function to the Checkbutton. This function will be called whenever the Checkbutton is checked or unchecked. Here is the code:
import tkinter as tk
root = tk.Tk()
# create function
def on_check():
if var.get() == 1:
print("Checked!")
else:
print("Unchecked.")
# create variable
var = tk.IntVar()
# create checkbutton
c = tk.Checkbutton(root, text="Check me!", variable=var, command=on_check)
c.pack()
root.mainloop()
In the code above, we first create a function named on_check
. This function checks the value of the var
variable and prints “Checked!” or “Unchecked.” depending on whether the Checkbutton is checked or unchecked. We then create an IntVar
variable named var
and a Checkbutton with the text “Check me!” and associate the var
variable with it using the variable
option. We also set the command
option to the on_check
function. This means that whenever the Checkbutton is checked or unchecked, the on_check
function will be called. We add the Checkbutton to the root
window using the pack()
method.
When we run the above code, we will see a Checkbutton with the text “Check me!” on the screen. Whenever we check or uncheck it, the corresponding message will be printed to the console.
Conclusion
In this article, we learned how to use the Checkbutton widget in Tkinter. We saw how to create Checkbuttons using the Checkbutton()
constructor, and how to configure them using various options. We also looked at some examples of configuring Checkbuttons, such as setting the variable
, onvalue
, and command
options. With this knowledge, we can now create more advanced GUI applications using Tkinter.