例如,在以下脚本中:
use Tk;
my $mw = new MainWindow;
my $t = $mw->Scrolled("Text")->pack;
my $popup = $mw->Menu(
-menuitems => [
[
Button => 'Copy Selected',
-state => "disabled",
-command => sub {$t->clipboardColumnCopy}
],
]
);
$t->menu($popup);
MainLoop;
如何判断选择何时发生,以便我可以使用以下代码
$popup->entryconfigure(1, -state=>'normal');
更改菜单项状态?
更新:
非常感谢@Chas和@gbacon:)
我想也许我也可以将两个好的答案结合起来:
$t->bind(
"<Button1-ButtonRelease>",
sub {
local $@;
my $state = defined eval { $t->SelectionGet } ?
"normal" : "disable";
$popup->entryconfigure(1, -state => $state)
}
);
答案 0 :(得分:2)
我不太了解Tk
,但这是一个答案(但可能不是正确答案):
#!/usr/bin/perl
use strict;
use warnings;
use Tk;
my $mw = new MainWindow;
my $t = $mw->Text->pack;
my $popup = $mw->Menu(
-menuitems => [
[ Button => 'Copy Selected', -state => "disabled", -command => sub {$t->clipboardColumnCopy} ],
]
);
$t->menu($popup);
$t->bind(
"<Button1-ButtonRelease>",
sub {
my $text = $t->getSelected;
if (length $text) {
$popup->entryconfigure(1, -state => 'normal');
} else {
$popup->entryconfigure(1, -state => 'disabled');
}
}
);
MainLoop;
答案 1 :(得分:2)
一些更改会产生您想要的行为。下面的代码会监视<ButtonPress-1>
,这可能会清除选择,如果是,则禁用“复制选定”。对于<ButtonPress-3>
,如果存在选择,则启用菜单项。
my $copySelectedLabel = "Copy Selected";
my $popup = $mw->Menu(
-menuitems => [
[
Button => $copySelectedLabel,
-state => "disabled",
-command => sub {$t->clipboardColumnCopy}
],
]
);
sub maybeEnableCopySelected {
local $@;
$popup->entryconfigure($copySelectedLabel, -state => "normal")
if defined eval { $t->SelectionGet };
}
sub maybeDisableCopySelected {
local $@;
$popup->entryconfigure($copySelectedLabel, -state => "disabled")
unless defined eval { $t->SelectionGet };
}
$t->bind('<ButtonPress-1>' => \&maybeDisableCopySelected);
$t->bind('<ButtonPress-3>' => \&maybeEnableCopySelected);
$t->menu($popup);