Book HomeMastering Perl/TkSearch this book

12.4. Classical Menubars

A classical menubar refers to the pre-Tk 8 idiom of arranging Menubutton widgets inside a Frame packed and stretched across the top of a MainWindow or Toplevel. Help Menubuttons are right justified on Unix machines. While you don't want to write new code in this style, it's important to know this idiom, because there's a lot of existing code written in this manner.

The following is typical classical menubar style, except for the use of -menuitems. Luckily for us, the three menu item subroutines are identical to our previous versions and generate a menubar that looks just like Figure 12-1. Most classical menubars create Menubuttons, Menus, and menu items as described in Section 12.2.1, "Menubars the Clunky, Casual, Old-Fashioned Way".

use subs qw/edit_menuitems file_menuitems help_menuitems/;

my $mw = MainWindow->new;

my $menubar = $mw->Frame(qw/-relief raised -borderwidth 2/);
$menubar->pack(qw/-fill x/);

my $file = $menubar->Menubutton(qw/-text File -underline 0/,
    -menuitems => file_menuitems);
my $edit = $menubar->Menubutton(qw/-text Edit -underline 0/,
    -menuitems => edit_menuitems);
my $help = $menubar->Menubutton(qw/-text Help -underline 0/, 
    -menuitems => help_menuitems);

$file->pack(qw/-side left/);
$edit->pack(qw/-side left/);
$help->pack(qw/-side right/);

12.4.1. Menubutton Options

The options specified with the Menubutton command (or via the configure method) can affect the Button part of the Menubutton, both the Button and the Menu, or just the Menu.[25] The options that affect the Menu are valid for the Menu widget as well as the Menubutton widget. Here is a brief synopsis of all the options and their effects. When the description says "Affects the Button only," the behavior is the same as it would be for a Button widget.

[25] The Menubutton widget comprises other widgets (in this case, Button and Menu) to provide the overall functionality.

-activebackground => color
Affects the background color of the button and the currently highlighted menu item.

-activeforeground => color
Affects the text color of the button and the currently highlighted menu item.

-anchor => 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w' | 'nw' | 'center'
Affects the button only. Changes the position of the text within the button.

-background => color
Affects the button and the menu. All the background color changes to color when the state of the button and the menu items is 'normal'.

-bitmap => bitmapname
Affects the button only. Displays bitmap instead of text.

-borderwidth => amount
Affects the button only. Changes the width of the button edges.

-cursor => cursorname
Affects the button only. Changes the cursor when it's over the button part of the menubutton.

-disabledforeground => color
Affects the button and the menu item text when the -state for either is 'disabled'.

-direction => "above" | "below" | "left" | "right" | "flush"
Tk 8 option only. The value "above" puts the menu above the menubutton, "below" puts it below the button, and "left" and "right" put it on the appropriate side of the button. "flush" puts the menu directly over the button.

-font => fontname
Affects the button only. Changes the font of any text displayed in the button.

-foreground => color
Affects the button only. Changes the color of any text or bitmap to color.

-height => amount
Affects the button only. Changes the height of the button.

-highlightbackground => color
Affects the button only. Changes the color of the highlight rectangle displayed around the button when the button does not have the keyboard focus.

-highlightcolor => color
Affects the button only. Changes the color of the highlight rectangle displayed around the button when the button has the keyboard focus.

-highlightthickness => amount
Affects the button only. Default is 0. Changes the width of the highlight rectangle around all edges of the button.

-image => imgptr
Affects the button only. Displays an image instead of text.

-indicatoron => 0 | 1
Affects the button; indirectly affects the display mechanism for the menu. When set to 1, a small bar appears on the right side of the button next to any text, bitmap, or image.

-justify => 'left' | 'right' | 'center'
Affects the button only. Changes the justification of the text within the button.

-menu => $menu
Tells the menubutton to display the menu associated with $menu instead of anything specified via the -menuitems option.

-menuitems => list
Causes the menu to display a list of items to create.

-padx => amount
Affects the button only. Adds extra space to the left and right of the button inside the button edge.

-pady => amount
Affects the button only. Adds extra space to the top and bottom of the button inside the button edge.

-relief => 'flat' | 'groove' | 'raised' | 'ridge' | 'sunken'
Affects the button only. The relief of the button changes to 'raised' when the button is pressed.

-state => 'normal' | 'active' | 'disabled'
Affects the button; indirectly affects the menu (the menu cannot be displayed if state is 'disabled').

-takefocus => 0 | 1 | undef
Affects the button only. Determines whether or not the button can have the keyboard focus.

-tearoff => 0 | 1
Affects the menu only. If set to 0, does not display the tearoff dashed line in the menu.

-text => text string
Affects the button only. Displays the specified string on the button (ignored if the -bitmap or -image option is used).

-textvariable => \$variable
Affects the button only. The information displayed in $variable is displayed on the button.

-underline => charpos
Affects the button only. The character at the integer charpos is underlined. If the button has the keyboard focus, pressing the Alt key causes the button that corresponds to the underlined character to be pressed.

-width => amount
Affects the button only. Changes the width of the button to amount.

-wraplength => pos
Affects the button only. Default is 0. Determines the screen distance for the maximum amount of text displayed on one line.

12.4.2. Button-Only Options

The following options affect only the button portion of the menubutton, and behave exactly as described in Chapter 4, " Button, Checkbutton, and Radiobutton Widgets": -cursor, -anchor, -bitmap, -borderwidth, -font, -foreground, -height, -highlightbackground, -highlightcolor, -highlightthickness, -image, -justify, -padx, -pady, -relief, -state, -takefocus, -text, -textvariable, -underline, -width, and -wraplength.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.