Tkinter add Checkbutton in LabelFrame
LabelFrame has a wide range of applications and is often used to organize option buttons or checkbuttons. The following is a direct example.
Tkinter add Checkbutton in LabelFrame Example#1
Frames the Checkbutton with a LabelFrame and sets the width and height of the root window.
from tkinter import *
def printInfo():
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)
root = Tk()
root.title("apidemos.com") # Title of the window
root.geometry("400x320")
# Create the following tagging framework with dictionaries
labFrame = LabelFrame(root,text="Please select the favorite sport")
sports = {0:"American Football",1:"Baseball",2:"Basketball",3:"Tennis",4:"Soccer",5:"Swimming"} # Dictionary of sports composition
checkboxes = {} # Dictionary, holding BooleanVar variables for the selected items
for i in range(len(sports)): # Create Checkbutton based on motion dictionary
checkboxes[i] = BooleanVar() # Creating Boolean variable objects
Checkbutton(labFrame,text=sports[i],
variable=checkboxes[i]).grid(row=i+1,sticky=W)
labFrame.pack(ipadx=5,ipady=5,pady=10)
btn = Button(root,text="OK",width=10,command=printInfo)
btn.pack()
root.mainloop()
Output: