submenu = Menu(edit, tearoff=True)
submenu.add_command(label='Spam', command=parent.quit, underline=0)
submenu.add_command(label='Eggs', command=notdone, underline=0)
edit.add_cascade(label='Stuff', menu=submenu, underline=0)
return menubar
if name == 'main':
root = Tk() # or TopLevel or Frame
root.title('menu_frm') # set window-mgr info
makemenu(root) # associate a menu bar
msg = Label(root, text='Frame menu basics') # add something below
msg.pack(expand=YES, fill=BOTH)
msg.config(relief=SUNKEN, width=40, height=7, bg='beige')
root.mainloop()
Again, let’s isolate the linkage logic here to avoid getting distracted by other details.
For the File menu case, here is what this boils down to:
menubar = Frame(parent) # make a Frame for the menubar
fbutton = Menubutton(menubar, text='File') # attach a Menubutton to Frame
file = Menu(fbutton) # attach a Menu to Menubutton
fbutton.config(menu=file) # crosslink button to menu
There is an extra Menubutton widget in this scheme, but it’s not much more complex
than making top-level window menus. Figures 9-5 and 9-6 show this script in action
on Windows.
Figure 9-5. menu_frm: Frame and Menubutton menu bar
The menu widgets in this script provide a default set of event bindings that automati-
cally pop up menus when selected with a mouse. This doesn’t look or behave exactly
like the top-level window menu scheme shown earlier, but it is close, can be configured
in any way that frames can (i.e., with colors and borders), and will look similar on every
platform (though this may or may not be a feature in all contexts).
The biggest advantage of frame-based menu bars, though, is that they can also be at-
tached as nested components in larger displays. Example 9-4 and its resulting interface
(Figure 9-7) show how—both menu bars are completely functional in the same single
window.
Menus | 513