Setting menu options using the Checkkutton button in Tkinter?

I make a menu using Tkinter, but I would like to put "add_checkbutton" instead of "add_command" in the menu options, but the problem is this: how do I uncheck or select the check box?

 menu = Menu(parent) parent.config(menu=menu) viewMenu = Menu(menu) menu.add_cascade(label="View", menu=viewMenu) viewMenu.add_command(label = "Show All", command=self.showAllEntries) viewMenu.add_command(label="Show Done", command= self.showDoneEntries) viewMenu.add_command(label="Show Not Done", command = self.showNotDoneEntries) 
+4
source share
1 answer

You need to bind the variable to the checkbutton element (s), then set the variable so that the element is checked or unchecked. For instance:

 import tkinter as tk parent = tk.Tk() menubar = tk.Menu(parent) show_all = tk.BooleanVar() show_all.set(True) show_done = tk.BooleanVar() show_not_done = tk.BooleanVar() view_menu = tk.Menu(menubar) view_menu.add_checkbutton(label="Show All", onvalue=1, offvalue=False, variable=show_all) view_menu.add_checkbutton(label="Show Done", onvalue=True, offvalue=0, variable=show_done) view_menu.add_checkbutton(label="Show Not Done", onvalue=1, offvalue=0, variable=show_not_done) menubar.add_cascade(label='View', menu=view_menu) parent.config(menu=menubar) parent.mainloop() 
+8
source

All Articles