2

例如在以下脚本中:

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)
    }
);
4

2 回答 2

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;
于 2010-09-01T13:42:18.127 回答
2

一些更改会产生您想要的行为。下面的代码<ButtonPress-1>可能会清除选择,如果是这样,则会禁用 Copy Selected。对于<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);
于 2010-09-01T14:10:39.937 回答