I am making a Menu using Tkinter, but I wanted to put "add_checkbutton"
instead of "add_command"
into the menu options, but problem is: how i deselect/select a checkbox?
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)
The Checkbutton widget is used to display a number of options to a user as toggle buttons. The user can then select one or more options by clicking the button corresponding to each option. You can also display images in place of text.
A Tearoff permits you to detach menus for the most window making floating menus. If you produce a menu you may see dotted lines at the top after you click a top menu item. To fix this tearoff needs to set to 0 at the time of menu declaration.
You need to associate a variable with the checkbutton item(s), then set the variable to cause the item to be checked or unchecked. For example:
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=0, variable=show_all)
view_menu.add_checkbutton(label="Show Done", onvalue=1, 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With