perltk:突出显示文本字段中的文本并更新标签

时间:2012-03-02 07:12:04

标签: perl tk perltk

在Windows XP上使用TK。

my $mw = new MainWindow;
my $text1 = $mw->Text(-width=>20, -height=>10)
            ->place(-x=>350, -y=>460); 

my $showlabel = $mw->Label(-text => "nothing selected")
            ->place(-x=>50, -y=>120);

$text1->configure( -command => sub { 
    $showlabel->configure(-text => "You selected:\t" . 
                          $text1->getSelected() 
                         )
  } 
);

运行代码后,每当我突出显示任何文本时,$showlabel都不会更新。 有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

编辑:没有按钮的代码。

不要忘记在程序结束时调用MainLoop来显示窗口。没有它,什么都不会发生。

试试这个:

use strict;
use warnings;

use Tk;

my $mw = new MainWindow;
my $text1 = $mw->Text(-width => 20, -height => 10)
                   ->place(-x => 350, -y => 460);
my $showlabel = $mw->Label(-text => "nothing selectd")
                       ->place(-x => 50, -y => 120);
$text1->bind('<KeyPress>'     , \&sel);
$text1->bind('<ButtonPress>'  , \&sel);
$text1->bind('<ButtonRelease>', \&sel);

MainLoop;

sub sel
{
    $showlabel->configure(-text => "You selected:\t" . $text1->getSelected);
}

答案 1 :(得分:1)

使用此:

$text1->bind( '<<Selection>>', sub {
  $showlabel->configure(-text => "You selected:\t".$text1->getSelected() )
} );