Tkinter Checkbutton Tutorial
The name of the checkbox in the Widget control is Checkbutton. checkbox appears as a box on the screen and the biggest difference between it and the option button is that it is checked. The most common way to design checkboxes is to have them in text form. As with labels, it is possible to design checkboxes that contain images.
The program can be designed so that each checkbox is tied to a function (or method), and when this option is selected, the associated function or method can be automatically executed. Alternatively, the program may be designed to have multiple sets of checkboxes, in which case it can be designed so that a set of checkboxes has an associated variable, from which the variable is bound to the set of checkboxes.
Checkbutton() method can be used to create checkboxes, and it is used as follows:
Checkbutton(master, options, ...)
The first argument of the Checkbutton() method is the parent object, indicating which parent object this checkbox will be built inside. The following are other commonly used options parameters within the Checkbutton( ) method.
(1) activebackground
: the background color when the mouse cursor is on the checkbox.
(2) activeforeground
: the foreground color when the mouse cursor is on the checkbox.
(3) bg
: the background color of the label background or indicator.
(4) bitmap
: bitmap image object.
(5) borderwidth or bd
: the border width is two pixels by default.
(6) command
: When the user changes the options, this function will be automatically executed.
(7) cursor
: when the mouse cursor on the check box when the cursor shape.
(8) disabledforeground
: when the color can not be operated.
(9) font
: the font shape.
(10) height
: the number of lines of text in the checkbox, the default is 1 line.
(11) highlightbackground
: the background color when the checkbox get focus.
(12) highlightcolor
: the color of the checkbox when it gets the focus.
(13) image
: image object, you can use this parameter if you want to create an option button with image.
(14) justify
: when containing multiple lines of text, the alignment of the last line of text.
(15) offvalue
: this is the control variable, the default value is 0 if the checkbox is not selected, you can change to set this value.
(16) onvalue
: this is the control variable, the default value is 1 if the checkbox is not selected, you can change this value.
(17) padx
: default is 1, you can set the interval between the checkbox and the text.
(18) pady
: default is 1, you can set the spacing between the top and bottom of the checkbox.
(19) relief
: the default is relief=FLAT, which can control the checkbox outer frame.
(20) selectcolor
: the color of the checkbox when it is selected.
(21) selectimage
: if the image checkbox is set, you can set a different image when the checkbox is selected.
(22) state
: the default is state=NORMAL, if it is set DISABLED then the checkbox is displayed in grayscale, indicating that it is temporarily unavailable. If the mouse cursor is above the checkbox, it means ACTIVE.
(23) text
: the text next to the checkbox.
(24) underline
: you can set the first few text with underline, counting from 0, the default is -1, which means no underline.
(25) variable
: set or get the currently selected checkbox, its value type is usually IntVar or StringVar.
(26) width
: the text of the checkbox has several characters width, it will adjust itself to the actual width when omitted.
(27) wraplength
: limit the number of text per line, the default is 0, indicating that only "\n" will be a new line.
Tkinter Checkbutton Tutorial Example#1
Create the Checkbutton application.
from tkinter import *
root = Tk()
root.title("apidemos.com")
lab = Label(root,text="Please select a favorite sport",fg="blue",bg="lightyellow",width=30)
lab.grid(row=0)
var1 = IntVar()
cbtnNFL = Checkbutton(root,text="American Football",variable=var1)
cbtnNFL.grid(row=1,sticky=W) # Left Alignment
var2 = IntVar()
cbtnMLB = Checkbutton(root,text="Baseball",variable=var2)
cbtnMLB.grid(row=2,sticky=W) # Left Alignment
var3 = IntVar()
cbtnNBA = Checkbutton(root,text="Basketball",variable=var3)
cbtnNBA.grid(row=3,sticky=W) # Left Alignment
root.mainloop()
Output:
If there are not many items in the checkbox, you can refer to the above example to build the checkbox items step by step using the Checkbutton() method. If there are many items, you can organize the items into a dictionary and then use a loop to build the checkboxes, refer to the following example.
Tkinter Checkbutton Tutorial Example#2
Store the sports checkbox items in the sports dictionary, then create the checkboxes, and when there are selected items, click the OK button to list the selected items in the Python Shell window.
from tkinter import *
def printInfo():
selection = ""
# print("before iteration:")
# print(selection)
for i in checkboxes.keys(): # Check the keywords in this dictionary, same as: for i in checkboxes:
if checkboxes[i].get() == True: # If selected, execute
selection = selection + sports[i] + "\t"
# print(selection)
# print("after iteration:")
print(selection)
root = Tk()
root.title("apidemos.com") # Title of the window
label = Label(root,text="Please select a favorite sport",fg="blue",bg="lightyellow",width=30)
label.grid(row=0)
sports = {0:"American Football",1:"Baseball",2:"Basketball",3:"Tennis",4:"Soccer",5:"Swimming"} # The sports dictionary is a checkbox for storing sports items.
checkboxes = {} # The checkboxes dictionary stores whether the option is selected or not.
for i in range(len(sports)): # Loop to turn the contents of thesports dictionary into a checkbox.
checkboxes[i] = BooleanVar() # Set the content of the checkboxes to BooleanVar objects, and only after this setting can you get its content with the get() method.
Checkbutton(root,text=sports[i],
variable=checkboxes[i]).grid(row=i+1,sticky=W)
btn = Button(root,text="OK",width=10,command=printInfo) # Create "OK" button, when click this button will execute printInfo( ) function, this function is mainly to print out the selected items.
btn.grid(row=i+2)
# btn.grid()
root.mainloop()
Output:
If you click the "OK" button in the above right image, you will see the following results in the Python Shell window.