Popup Menus
Perl/Tk provides several ways to pop up menus. Thus far, all menus have been posted automatically by pressing a menubutton, but we can do it ourselves by binding keystrokes or button clicks to callbacks.
The post and Post Methods
The lowest-level
mechanism is the Menu post
method, which posts a
menu at a specific screen coordinate. The Post
method works like post
, but additionally activates
a specific menu item. In either case, we are responsible for
determining where the menu is displayed.
Figure 12-5. Post posts a menu and activates a menu item (here, index 4)
Let’s start with the code that produced Figure 12-5. It creates one menu, then displays it several ways. The toolbar across the top of the window contains a left-justified Menubutton and a right-justified labeled Optionmenu. The menu contains nine menu items, four separators, and five command menus at indexes 0, 2, 4, 6, and 8. Of course, the first way to post the menu is to press the File menubutton.
my $toolbar = $mw->Frame->pack(qw/-fill x -expand 1/); my $file = $toolbar->Menubutton( -text => 'File', -relief => 'raised', ); $file->pack(qw/-side left/);; my $menu = $file->Menu(-tearoff => 0, -menuitems => [ [qw/command ~New/], '', [qw/command ~Open/], '', [qw/command ~Save/], '', [qw/command ~Close/], '', [qw/command ~Quit/, -command => \&exit], ]); $file->configure(-menu => $menu); my $menu_index = 0; my ...
Get Mastering Perl/Tk 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.