O'Reilly logo

Mastering Perl/Tk by Nancy Walsh, Stephen Lidie

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

Post posts a menu and activates a menu item (here, index 4)

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required