9.3 Creating Menus with Tkinter


Credit: Luther Blissett

9.3.1 Problem

You want to create a window that has a menu bar at the top.

9.3.2 Solution

Use the Tkinter Menu widget:

import sys from Tkinter import * root = Tk(  ) # Insert a menu bar on the main window menubar = Menu(root) root.config(menu=menubar) # Create a menu button labeled "File" that brings up a menu filemenu = Menu(menubar) menubar.add_cascade(label='File', menu=filemenu) # Create entries in the "File" menu # simulated command functions that we want to invoke from our menus def doPrint(  ): print 'doPrint' def doSave(  ): print 'doSave' filemenu.add_command(label='Print', command=doPrint) filemenu.add_command(label='Save', command=doSave) filemenu.add_separator(  ) filemenu.add_command(label='Quit', command=sys.exit) root.mainloop(  )

9.3.3 Discussion

Menus in Tkinter applications are handled entirely by the Menu widget. As shown in the recipe, you use Menu both for the top-level menu bar (which you add to a top-level window as its menu configuration setting) and for cascading menus (which you add to the menu bar, or to other menus, with the add_cascade method).

A menu can have several kinds of entries. A cascade entry pops up a submenu when the user selects it, and is added with add_cascade. A command entry calls a function when the user selects it, and is added with add_command. A separator visually separates other entries, and is added with add_separator.

A checkbutton entry is added with add_checkbutton and has an associated Tkinter IntVar, with an on value and an off value. If the associated variable has the on value, the entry displays a check besides its value; if it has the off value, it doesn't. When the user selects the entry, this toggles the state of the variable:

vdebug = IntVar(  ) filemenu.add_checkbutton(label='Debug', var=vdebug)

You can access the value of vdebug by calling vdebug.get and set it to any integer value n by calling vdebug.set(n). A checkbutton entry can also optionally have a command to call a function when the user selects it.

A group of radiobutton entries is associated with a single IntVar instance. Only one radiobutton associated with that variable can be on at any time. Selecting a radiobutton gives the variable the value associated with it:

vlevel = IntVar(  ) filemenu.add_radiobutton(label='Level 1', var=vlevel, value=1) filemenu.add_radiobutton(label='Level 2', var=vlevel, value=2) filemenu.add_radiobutton(label='Level 3', var=vlevel, value=3)

A radiobutton entry can also optionally have a command to call a function when the user selects it.

9.3.4 See Also

Information about Tkinter can be obtained from a variety of sources, such as Pythonware's An Introduction to Tkinter, by Fredrik Lundh (http://www.pythonware.com/library), New Mexico Tech's Tkinter reference (http://www.nmt.edu/tcc/help/lang/python/docs.html), and various books.



Python Cookbook
Python Cookbook
ISBN: 0596007973
EAN: 2147483647
Year: 2005
Pages: 346

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net