Adding the Menu Bar to Main Window : Menu « GUI « Perl






Adding the Menu Bar to Main Window

 
          
#!/usr/bin/perl -w

use Tk;

$main = MainWindow->new();
$menubar = $main->Frame(-relief=>"raised",
                        -borderwidth=>2);

$filebutton = $menubar->Menubutton(-text=>"File",
                                   -underline => 0);  # F in File

$filemenu = $filebutton->Menu();
$filebutton->configure(-menu=>$filemenu);

$filemenu->command(-command => \&open_choice,
                   -label => "Open...",
                   -underline => 0); # O in Open

$filemenu->separator();

$filemenu->command(-label => "Exit",
                   -command => \&exit_choice,
                   -underline => 1);  # "x" in Exit

$helpbutton = $menubar->Menubutton(-text=>"Help",
                                   -underline => 0);  # H in Help

$helpmenu = $helpbutton->Menu();

$helpmenu->command(-command => \&about_choice,
                   -label => "About TkMenu...",
                   -underline => 0); # A in About

$helpbutton->configure(-menu=>$helpmenu);
$filebutton->pack(-side=>"left");
$helpbutton->pack(-side=>"right");
$menubar->pack(-side=>"top", -fill=>"x");
$label = $main->Label(-text => "Main Area");

$label->pack(-side=>"top", 
             -expand=>1,
             -padx=>100, 
             -pady=>100);
MainLoop();
sub exit_choice {
    print "You chose the Exit choice!\n";
    exit;
}

sub open_choice {
    print "Open file\n";
}

sub about_choice {
    print "About tkmenu.pl\n";
}

   
  








Related examples in the same category

1.Build menu items for main windows
2.CheckBox Menu
3.RadioButton Menu
4.Adding MenuItem to Menu Bar
5.Adding Action to MenuItem
6.Packing the MenuItem on a Menu Bar: left
7.Packing the MenuItem on a Menu Bar: right
8.Set Text for MenuItem
9.Setting the Underline Character for MenuItem
10.Adding Separator