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

Menubars and Pulldown Menus

Perhaps the best way to learn about the menu system is to examine the program that produced Figures Figure 12-1 and Figure 12-2. We’re going to show two ways to create those menus, first using a straightforward strategy and then a more elegant one.

Menubars the Clunky, Casual, Old-Fashioned Way

Here’s one way to do it using Perl/Tk 8. We begin by creating a normal Menu and configuring it as the MainWindow’s menubar:

use Tk 800.000;

my $mw = MainWindow->new;
$mw->configure(-menu => my $menubar = $mw->Menu);

Now create the cascade menubuttons. We save each menubutton’s reference—an object of type Tk::Menu::Cascade—so we can add the requisite menu items later. (Note that unlike almost every other Perl/Tk object, a Tk::Menu::Cascade object is built from an array rather than a hash.) Each menubutton is assigned an identifying label that’s displayed on the menubutton. The tilde character (~) represents the -underline character and is merely a convenience feature.

We already know the menu system handles a Toplevel menu specially. Since we didn’t specify -tearoff => 0 when the menu was created, there’s an implicit tearoff at index 0, meaning that the File, Edit, and Help cascades are menu item indexes 1, 2, and 3, respectively. Further, menubutton cascades are arranged from left to right rather than from top to bottom.

my $file = $menubar->cascade(-label => '~File'); my $edit = $menubar->cascade(-label => '~Edit'); my $help = $menubar->cascade(-label => '~Help'); ...

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