What is the Python Tkinter Check Button?
A Check Button is a Python Tkinter GUI widget that presents to the user a set of predefined options in the form of boxes. The user is allowed to select more than one available option.
Check Button Syntax
ChkButton = Checkbutton(master, option.........)
Options
List of all relevant options available for Check Buttons
No. | Option | Description |
---|---|---|
1 | activebackground | Color of the background when widget is under focus. |
2 | activeforeground | Color of the foreground when widget in under focus. |
3 | bg | Background color for the area around the widget. |
4 | bd | Size of the border around the widget. Default value is 2 pixels. |
5 | command | If the state of this widget is changed, this procedure is called. |
6 | cursor | When the mouse is hovering over this widget, it can be changed to a special cursor type like an arrow or dot. |
7 | font | The type of font to be used for this widget. |
8 | fg | The color for the text. |
9 | height | The height of the widget in terms of text lines. |
10 | highlightcolor | The text color when the widget is under focus. |
11 | image | Used to display an image |
12 | justify | Specifies how the text is aligned within the widget. One of “left”, “center”, or “right”. |
13 | padx | Amount of padding in terms of pixels in the left-right directions. |
14 | pady | Amount of padding in terms of pixels for the area above and below the widget. |
15 | relief | It specifies the type of the border. Default is Flat, other options include RAISED and SUNKEN . |
16 | State | Default is NORMAL. DISABLED causes the Widget to gray out and go inactive. ACTIVE is the state of the Widget when mouse is over it. |
17 | text | The text displayed next to the Checkbutton. |
18 | variable | The variable that tracks the current state of the checkbutton. |
19 | width | Width of the Checkbutton. |
Creating a Check Button
We’ve created a simple check button here using some of the options shown in the table above.
from tkinter import *
root = Tk()
root.geometry("200x150")
frame = Frame(root)
frame.pack()
Var1 = IntVar()
Var2 = IntVar()
ChkBttn = Checkbutton(frame, text = "Option1", variable = Var1)
ChkBttn.pack(padx = 5, pady = 5)
ChkBttn2 = Checkbutton(frame, text = "Option2", variable = Var2)
ChkBttn2.pack(padx = 5, pady = 5)
root.mainloop()
You can pass certain parameters into the pack()
function such as padx
and pady
. This ensures that there remains some distance between the two widgets.
Retrieving Check Button Values
A Check Button returns 1 if it’s checked, otherwise it returns 0.
There are two ways to retrieve Check Button values. The first involves the use of the get()
function. This method is user-triggered, meaning unless the user triggers the appropriate command the values will not be returned.
In this case, the User has to press the Submit button for the values to be returned. The submit button calls the retrieve function which returns the prints the check button values.
from tkinter import *
def retrieve():
print(Var1.get())
print(Var2.get())
root = Tk()
root.geometry("200x150")
frame = Frame(root)
frame.pack()
Var1 = IntVar()
Var2 = IntVar()
ChkBttn = Checkbutton(frame, text = "Option1", variable = Var1)
ChkBttn.pack(padx = 5, pady = 5)
ChkBttn2 = Checkbutton(frame, text = "Option2", variable = Var2)
ChkBttn2.pack(padx = 5, pady = 5)
Button = Button(frame, text = "Submit", command = retrieve)
Button.pack(padx = 5, pady = 5)
root.mainloop()
The second method involves the use of the inbuilt option within the CheckButton called command
. It functions very similar to the example above.
In this case, no button is required. Every time the user changes the state of the check button (by selecting or selecting), this command will call the function it has been assigned.
from tkinter import *
def retrieve():
print(Var1.get())
print(Var2.get())
root = Tk()
root.geometry("200x150")
frame = Frame(root)
frame.pack()
Var1 = IntVar()
Var2 = IntVar()
ChkBttn = Checkbutton(frame, text = "Burger", variable = Var1, command = retrieve)
ChkBttn.pack(padx = 5, pady = 5)
ChkBttn2 = Checkbutton(frame, text = "Pizza", variable = Var2, command = retrieve)
ChkBttn2.pack(padx = 5, pady = 5)
root.mainloop()
Remember to leave out the parenthesis or “()” while assigning the function to the command parameter.
This concludes our Python Tkinter Check Button article. Any suggestions or contributions for CodersLegacy are more than welcome. Relevant questions regarding the article can be asked in the comments section below.
You can head back to the main Tkinter article using this link to learn about the other widgets.