Speaking of which, the recommended way to figure out which platform you're running on is:
root.tk.call('tk', 'windowingsystem') # will return x11, win32 or aqua
Menubars
Before you Start
root.option_add('*tearOff', FALSE)
Creating a Menubar
In Tk, menubars are associated with individual windows; each toplevel window can have at most onemenubar.
To actually create a menubar for a window, we first create a menu widget, and then use thewindow's"menu" configuration option to attach the menu widget to the window.As noted, the menu widget must be a child of the toplevel window.
win = Toplevel(root)
menubar = Menu(win)
win['menu'] = menubar
Adding Menus
menubar = Menu(parent)
menu_file = Menu(menubar)
menu_edit = Menu(menubar)
menubar.add_cascade(menu=menu_file, label='File')
menubar.add_cascade(menu=menu_edit, label='Edit')
Adding Menu Items
menu_file.add_command(label='New', command=newFile)
menu_file.add_command(label='Open...', command=openFile)
menu_file.add_command(label='Close', command=closeFile)
So adding menu items to a menu is essentially the same as adding a submenu, but ratherthan adding a menu item of type "cascade", we're adding one of type "command".
Types of Menu Items
A third type of menu item is the "separator", which produces the dividing lineyou often see between different sets of menu items.
menu_file.add_separator()
Finally, there are also
"checkbutton" and
"radiobutton" menu items,which behave analogously to checkbutton and radiobutton widgets. These menu itemshave a variable associated with them, and depending on the value of that variable,will display an indicator (i.e. a checkmark or a selected radiobutton) next to theitem's label.
check = StringVar()
menu_file.add_checkbutton(label='Check', variable=check, onvalue=1, offvalue=0)
radio = StringVar()
menu_file.add_radiobutton(label='One', variable=radio, value=1)
menu_file.add_radiobutton(label='Two', variable=radio, value=2)
When the user selects a checkbutton item that is not already checked, it will set theassociated variable to the value in
"onvalue", while selecting a item thatis already checked sets it to the value in
"offvalue". Selecting a radiobuttonitem sets the associated variable to the value in
"value". Both types of itemsalso react to changes in the associated variable from within other parts of your program.
As with command items, checkbutton and radiobutton menu items do accept a "command"configuration option, that will be invoked when the menu item is selected; the associatedvariable, and hence the menu item's state, is updated before the callback is invoked.
Accelerator Keys
The "accelerator" option is used to indicate the menu accelerator that should beassociated with this menu. This does not actually create the accelerator, butonly displays what it is next to the menu item. You still need to create a binding forthe accelerator yourself.
Querying and Changing Item Options
print( menu_file.entrycget(0, 'label') )
menu_file.entryconfigure('Close', state=DISABLED)
print( menu_file.entryconfigure(0) )
Platform Menus
Mac OS X
apple = Menu(menubar, name='apple')
help = Menu(menubar, name='help')
menubar.add_cascade(menu=apple)
menubar.add_cascade(menu=help)
Windows
sysmenu = Menu(menubar, name='system')
menubar.add_cascade(menu=sysmenu)
X11
menu_help = Menu(menubar, name='help')
menubar.add_cascade(menu=menu_help, label='Help')
Contextual Menus
To create a contextual menu, you'll use exactly the same commands you did to create menus in the menubar.
To activate the menu, the user will use a contextual menu click, which you will have to bind to.
from tkinter import *
root = Tk()
menu = Menu(root)
for i in ('One', 'Two', 'Three'):
menu.add_command(label=i)
if (root.tk.call('tk', 'windowingsystem')=='aqua'):
root.bind('<2>', lambda e: menu.post(e.x_root, e.y_root))
root.bind('<Control-1>', lambda e: menu.post(e.x_root, e.y_root))
else:
root.bind('<3>', lambda e: menu.post(e.x_root, e.y_root))
from:
http://www.tkdocs.com/tutorial/menus.html