我一直在关注Tkx::Tutorial来尝试学习如何使用 Perl 制作 GUI。我到了它有一个如何创建菜单并遵循它的示例的部分。除了一项功能外,它几乎完全正确。这些-underline
部分似乎无法正常工作。
-underline => 0
应该在菜单文本的第一个字符下划线,并允许您使用第一个字母作为键盘快捷键,而不必单击它。
代码如下所示:
#!/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.",
);
}
此代码将创建以下窗口:
使用“F”作为键盘快捷键无法打开文件菜单,使用“H”无法打开帮助菜单
(注意“New”函数目前什么都不做,所以对它的调用被破坏了。现在应该是这种方式,所以忽略它。)
单击“文件”菜单后,将打开第二个菜单。在这里,键盘快捷键似乎可以工作,“N”和“E”将执行它们相应的命令,但它们仍然没有正确加下划线。
为什么这不按他们应该的方式工作?