Menus
Class Menu
implements
all kinds of menus: menubars of top-level windows, submenus, and
pop-up menus. To use a Menu
instance
m
as the menubar for a top-level window
w
, set
w
’s configuration option
menu=
m
. To use
m
as a submenu of a
Menu
instance x
, call
x
.add_cascade
with a
named argument menu=
m
.
To use m
as a pop-up menu, call method
m
.post
.
Besides configuration options covered in Section 16.2.1 earlier in this chapter, a
Menu
instance m
supports option
postcommand=
callable
.
Tkinter
calls callable
without arguments each time it is about to display
m
(whether because of a call to
m
.post
or because of
user actions). You can use this option to update a dynamic menu just
in time when necessary.
By default, a Tkinter
menu shows a tear-off entry
(a dashed line before other entries), which lets the user get a copy
of the menu in a separate Toplevel
window. Since
such tear-offs are not part of user interface standards on popular
platforms, you may want to disable tear-off functionality by using
configuration option tearoff=0
for the menu.
Menu-Specific Methods
Besides methods common to all widgets, an instance
m
of class Menu
supplies several menu-specific methods.
Menu Entries
When
a menu m
displays, it shows a vertical
(horizontal for a menubar) list of entries. Each entry can be one of
the following kinds:
-
cascade
A submenu; option
menu=
x
must give asx
anotherMenu
instance-
checkbutton
Similar to a
Checkbutton
widget; typical options arevariable
(which must indicate a ...
Get Python in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.