Python Tkinter Check Button


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.OptionDescription
1activebackgroundColor of the background when widget is under focus.
2activeforegroundColor of the foreground when widget in under focus.
3bgBackground color for the area around the widget.
4bdSize of the border around the widget. Default value is 2 pixels.
5commandIf the state of this widget is changed, this procedure is called.
6cursorWhen the mouse is hovering over this widget, it can be changed to a special cursor type like an arrow or dot.
7font The type of font to be used for this widget.
8fg The color for the text.
9heightThe height of the widget in terms of text lines.
10highlightcolorThe text color when the widget is under focus.
11imageUsed to display an image
12justifySpecifies how the text is aligned within the widget. One of “left”, “center”, or “right”.
13padxAmount of padding in terms of pixels in the left-right directions.
14padyAmount of padding in terms of pixels for the area above and below the widget.
15reliefIt specifies the type of the border. Default is Flat, other options include RAISED and SUNKEN.
16StateDefault is NORMAL. DISABLED causes the Widget to gray out and go inactive. ACTIVE is the state of the Widget when mouse is over it.
17textThe text displayed next to the Checkbutton.
18variableThe variable that tracks the current state of the checkbutton.
19widthWidth 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, width = 15, variable = Var1)
ChkBttn.pack(padx = 5, pady = 5)

ChkBttn2 = Checkbutton(frame, width = 15, variable = Var2)
ChkBttn2.pack(padx = 5, pady = 5)

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.

Tkinter widgets - Check button

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()
Python Tkinter Check Button
Check Button

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.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments