The 'Checkbutton' widget is used to create a multiple-choice interface by assigning each button a variable of its own.
import tkinter as tk root = tk.Tk() # root widget def var_states(): # values are 0 (unchecked) or 1 (checked) print("male: {}, female: {}".format(var1.get(), var2.get())) var1 = tk.IntVar() button1 = tk.Checkbutton(root, text="male", variable=var1, command=lambda: print("male changed")) button1.grid(row=0, column=0) var2 = tk.IntVar() button2 = tk.Checkbutton(root, text="female", variable=var2, command=lambda: print("female changed")) button2.grid(row=0, column=1) button3 = tk.Button(root, text='Show', command=var_states) button3.grid(row=0, column=2) button4 = tk.Button(root, text='Quit', command=root.quit) button4.grid(row=0, column=3) root.mainloop()
import tkinter as tk root = tk.Tk() # root widget states = [] for c in range(10): var = tk.IntVar() button = tk.Checkbutton(root, text=str(c), variable=var, command=lambda: print([var.get() for var in states])) button.grid(row=0, column=c) states.append(var) # keep reference root.mainloop()
Selected keyword arguments in Checkbutton() master=root # the first argument text="text label" textvariable=strvar # an alternative to 'text' variable=var # a variable linked to the checkbutton command=lambda: print("message") onvalue=1 # values reflecting the checked (default 1) offvalue=0 # and unchecked (default 0)