Previous
Listbox 
Next
Canvas, Message, Adjuster, Scrolled 
Perl/Tk Tutorial - Create GUI with Perl's Tk Module
Menubutton, Menu

Widgets 5 : Menubutton, Menu, Optionmenu

Menubutton

A menubutton is a widget that displays a textual string, bitmap, or image and is associated with a menu widget.In normal usage, pressing left-clicking the menubutton causes the associated menu to be posted just underneath the menubutton.

Some Options
-direction => DIRECTION Specifies where the menu is going to be popup up. above tries to pop the menu above the menubutton. below tries to pop the menu below the menubutton. left tries to pop the menu to the left of the menubutton. right tries to pop the menu to the right of the menu button. flush pops the menu directly over the menubutton.
-menu => NAME Specifies the path name of the menu associated with this menubutton. The menu must be a child of the menubutton.



Menu

A menu is a widget that displays a collection of one-line entries arranged in one or more columns. There exist several different types of entries, each with different properties. Entries of different types may be combined in a single menu. Menu entries are not the same as entry widgets. In fact, menu entries are not even distinct widgets; the entire menu is one widget.

Some Options
-tearoff => BOOLEAN This option must have a proper boolean value, which specifies whether or not the menu should include a tear-off entry at the top. If so, it will exist as entry 0 of the menu and the other entries will number starting at 1. The default menu bindings arrange for the menu to be torn off when the tear-off entry is invoked.
-title => STRING The string will be used to title the window created when this menu is torn off. If the title is NULL, then the window will have the title of the menubutton or the text of the cascade item from which this menu was invoked.
-type => OPTION This option can be one of menubar, tearoff, or normal, and is set when the menu is created. While the string returned by the configuration database will change if this option is changed, this does not affect the menu widget's behavior.


Some Commands
SyntaxDescription
$widget -> TYPE(?option=>
value,option=>value,...?);
Add a new entry to the bottom of the menu. The new entry's type is given by TYPE and must be one of cascade, checkbutton, command, radiobutton, or separator, or a unique abbreviation of one of the above. If additional arguments are present, they specify any of the following options:
-accelerator => VALUE Specifies a string to display at the right side of the menu entry. Normally describes an accelerator keystroke sequence that may be typed to invoke the same function as the menu entry. This option is not available for separator or tear-off entries.
-columnbreak => VALUE When this option is zero, the appears below the previous entry. When this option is one, the menu appears at the top of a new column in the menu
-label => VALUE Specifies a string to display as an identifying label in the menu entry. Not available for separator or tear-off entries.
-compound => VALUE Specifies whether the menu entry should display both an image and text, and if so, where the image should be placed relative to the text. Valid values for this option are bottom, center, left, none, right and top.
-image => VALUE Specifies an image to display in the menu instead of a text string or bitmap The image must have been created by some previous invocation of image create. This option overrides the -label and -bitmap options but may be reset to an empty string to enable a textual or bitmap label to be displayed. This option is not available for separator or tear-off entries.
-underline => VALUE Specifies the integer index of a character to underline in the entry. This option is used to make keyboard shortcuts. 0 corresponds to the first character of the text displayed in the entry, 1 to the next character, and so on.
$widget -> delete(index1,
?index2?
);
Delete all of the menu entries between index1 and index2 inclusive. If index2 is omitted then it defaults to index1. Attempts to delete a tear-off menu entry are ignored (instead, you should change the tearOff option to remove the tear-off entry).
$widget -> insert(index,type, ?option=>value ...?); Same as the add widget command except that it inserts the new entry just before the entry given by index, instead of appending to the end of the menu. The type, option, and value arguments have the same interpretation as for the add widget command. It is not possible to insert new menu entries before the tear-off entry, if the menu has one.


Example

#!/usr/local/bin/perl
use Tk;
# Main Window
my $mw = new MainWindow;

#Making a text area
my $txt = $mw -> Scrolled('Text',-width => 50,-scrollbars=>'e') -> pack ();

#Declare that there is a menu
my $mbar = $mw -> Menu();
$mw -> configure(-menu => $mbar);

#The Main Buttons
my $file = $mbar -> cascade(-label=>"File", -underline=>0, -tearoff => 0);
my $others = $mbar -> cascade(-label =>"Others", -underline=>0, -tearoff => 0);
my $help = $mbar -> cascade(-label =>"Help", -underline=>0, -tearoff => 0);

## File Menu ##
$file -> command(-label => "New", -underline=>0, 
		-command=>sub { $txt -> delete('1.0','end');} );
$file -> checkbutton(-label =>"Open", -underline => 0,
		-command => [\&menuClicked, "Open"]);
$file -> command(-label =>"Save", -underline => 0,
		-command => [\&menuClicked, "Save"]);
$file -> separator();
$file -> command(-label =>"Exit", -underline => 1,
		-command => sub { exit } );

## Others Menu ##
my $insert = $others -> cascade(-label =>"Insert", -underline => 0, -tearoff => 0);
$insert -> command(-label =>"Name", 
	-command => sub { $txt->insert('end',"Name : Binny V A\n");});
$insert -> command(-label =>"Website", -command=>sub { 
	$txt->insert('end',"Website : http://www.geocities.com/binnyva/\n");});
$insert -> command(-label =>"Email", 
	-command=> sub {$txt->insert('end',"E-Mail : binnyva\@hotmail.com\n");});
$others -> command(-label =>"Insert All", -underline => 7,
	-command => sub { $txt->insert('end',"Name : Binny V A
Website : http://www.geocities.com/binnyva/
E-Mail : binnyva\@hotmail.com");
  	});

## Help ##
$help -> command(-label =>"About", -command => sub { 
	$txt->delete('1.0','end');
	$txt->insert('end',
	"About
----------
This script was created to make a menu for a\nPerl/Tk tutorial.
Made by Binny V A
Website : http://www.geocities.com/binnyva/code
E-Mail : binnyva\@hotmail.com"); });

MainLoop;

sub menuClicked {
	my ($opt) = @_;
	$mw->messageBox(-message=>"You have clicked $opt.
This function is not implanted yet.");
}

Create the main buttons as cascade menus and create the menus as their slaves. For more information see the manual.


Optionmenu

Makes a button, which when clicked on shows a list with available options. Useful when user has to make one choice when multiple choices are given. Below is a options menu in HTML. A word of caution though - Perl/Tk's option menu has a very different appearance.

Syntax
my $widget = $mw -> Optionmenu(?option=>value,option=>value,...?);

Options
SyntaxDescription
-options=>OPTIONS(Re)sets the list of options presented.
-command=>CALLBACKDefines the callback that is invokes when a new option is selected.
-variable=>\$VARIABLEReference to a scalar that contains the current value of the selected option.


Methords
SyntaxDescriptionExample
$widget -> addOptions([Option1=>Value1], ?[Option2=>Value2]?); Adds newly given options to the already available options. $opt->addOptions([May=>5], [June=>6], [July=>7], [Augest=>8]);


Example...

#!/usr/local/bin/perl
use Tk;
# Main Window
$mw = new MainWindow;

my $var;
my $opt = $mw -> Optionmenu(-options => [qw(January February March April)],
        -command => sub { print "got: ", shift, "\n" },
        -variable => \$var,
        )->pack;
$opt->addOptions([May=>5],[June=>6],[July=>7],[Augest=>8]);

$mw->Label(-textvariable=>\$var, -relief=>'groove')->pack;
$mw->Button(-text=>'Exit', -command=>sub{$mw->destroy})->pack;

MainLoop;
Previous
Listbox 
Next
Canvas, Message, Adjuster, Scrolled 
Subscribe to Feed