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 に感謝します :)

2つの良い答えを組み合わせることもできると思います:

$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>は、選択をクリアする可能性があることを監視し、その場合は [選択項目のコピー] を無効にします。の場合<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 に答える