Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Menu options with Checkbutton in Tkinter?

Tags:

python

tkinter

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)
like image 946
itsaboutcode Avatar asked Oct 14 '10 01:10

itsaboutcode


People also ask

What is Checkbutton in tkinter?

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.

What is Tearoff in tkinter?

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.


1 Answers

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()
like image 139
Bryan Oakley Avatar answered Sep 19 '22 22:09

Bryan Oakley