I have been following the Tkx::Tutorial to try and learn how to make a GUI using Perl. I got to the section where it has an example of how to create a menu and followed it. It works almost entirely correct except for one feature. The -underline
sections don't seem to be working correctly.
-underline => 0
is supposed to underline the first character of the menu text and allow you to use the first letter as a keyboard shortcut instead of having to click on it.
The code is shown below:
#!/usr/bin/perl
use strict;
use warnings;
use Tkx;
our $VERSION = '1.00';
(my $progname = $0) =~ s,.*[\\/],,;
my $IS_AQUA = Tkx::tk_windowingsystem() eq "aqua";
Tkx::package_require('style');
Tkx::style__use('as', -priority => 70);
my $mw = Tkx::widget->new('.');
$mw->configure(-menu => mk_menu($mw));
Tkx::MainLoop();
exit;
sub mk_menu {
my $mw = shift;
my $menu = $mw->new_menu;
my $file = $menu->new_menu(
-tearoff => 0,
);
$menu->add_cascade(
-label => 'File',
-underline => 0,
-menu => $file,
);
$file->add_command(
-label => 'New',
-underline => 0,
-accelerator => 'Ctrl+N',
-command => \&new,
);
$mw->g_bind('<Control-n>', \&new);
$file->add_command(
-label => 'Exit',
-underline => 0,
-command => [\&Tkx::destroy, $mw],
) unless $IS_AQUA;
my $help = $menu->new_menu(
-name => 'help',
-tearoff => 0,
);
$menu->add_cascade(
-label => 'Help',
-underline => 0,
-menu => $help,
);
$help->add_command(
-label => "\u$progname Manual",
-command => \&show_manual,
);
my $about_menu = $help;
if ($IS_AQUA) {
# On Mac OS we want about box to appear in the application
# menu. Anything added to a menu with the name "apple" will
# appear in this menu.
$about_menu = $menu->new_menu(
-name => 'apple',
);
$menu->add_cascade(
-menu => $about_menu,
);
}
$about_menu->add_command(
-label => "About \u$progname",
-command => \&about,
);
return $menu;
}
sub about {
Tkx::tk___messageBox(
-parent => $mw,
-title => "About \u$progname",
-type => 'ok',
-icon => 'info',
-message => "$progname v$VERSION\n" .
"Copyright 2005 ActiveState. " .
"All rights reserved.",
);
}
This code will create the following window:
Using "F" as a keyboard shortcut does not work to open the file menu and using "H" does not open the help menu
(Note the "New" function currently does nothing so the call to it is broken. This is supposed to be this way for now so just ignore it.)
After clicking on the "File" menu a second menu will open. Here the keyboard shortcuts appear to work "N" and "E" will execute their corresponding commands but they are still not properly underlined.
Why isn't this working they way it is supposed to?
Holding ALT solved the problem.
Thanks @gangabass!