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.
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);
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 -
character and is merely a convenience feature.
We already know the menu system handles a Toplevel menu specially.
Since we didn’t specify -
=> 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'); ...