0

有人可以用我的问题作为标题吗?我在创建的表单中有文本和条目小部件,但不知何故,我希望如果我可以为某些文本和条目小部件做一些事情,我会在其中放置“”字样,如果用户想要使用该条目,他们只需鼠标单击该列,“”字样将自动清除。我可以知道该怎么做吗?这是我没有单击鼠标即可清除功能的代码。谢谢。

#This section apply text  widget.
$mwf->Label(-text => 'Waiver',
    -justify => 'left'
    )->grid(-sticky => 'w', -column => 0, -row => 8);


my $scrollbar = $mwf->Scrollbar() 
->grid( -sticky => 'ns',-column=>2, -row => 8);

my $waiver = $mwf->Text(-height => 5,
        -width => 100,
        -background => "white",
        -wrap => 'word',
        -yscrollcommand => ['set' => $scrollbar], 
        )->grid(-sticky => 'w', -column => 1, -row => 8);

#This section apply entry widget.
$mwf->Label(-text => 'Exclude View',
-justify => 'left'
)->grid(-sticky => 'w', -column => 0, -row => 10);

my $exclude = $mwf->Entry(-width => 100,
        -background => "white",
        )->grid(-sticky => 'w', -column => 1, -row => 10);
        push @entries, $exclude ;
$exclude -> insert('end', '<optional>') ;
4

1 回答 1

2

您可以使用触发事件时调用的绑定

格式$widget->bind('<event>' => callback);

请参阅下面的示例程序

#!/usr/bin/perl

use strict;
use warnings;
use Tk;
use Tk::Entry;
use Tk::TextUndo;

my $win = new MainWindow;
$win->geometry("400x300");

my $entry = $win->Entry()->pack;
my $text = $win->TextUndo()->pack;
$text->insert('end', 'default');
$entry->insert('end', 'default');

$text->bind('<FocusIn>' => \&textfocus);
$entry->bind('<FocusIn>' => \&entryfocus);
MainLoop;

sub textfocus {

    my $info = $text->Contents();
    if($info =~ /^default$/){
        $text->delete('1.0', 'end');
    }
}

sub entryfocus {
    my $info = $entry->get();
    if($info =~ /^default$/){
        $entry->delete('0.0', 'end');
    }
}

有关 perl\tk 事件的更多信息:http: //docstore.mik.ua/orelly/perl3/tk/ch15_02.htm#INDEX-2191

编辑:

当事件被触发时,调用小部件的引用被传递给回调。下面是一种为每个小部件仅使用一个回调子的方法。

$text->bind('<FocusIn>' => \&w_focus);
$entry->bind('<FocusIn>' => \&w_focus);
MainLoop;

sub w_focus {
    my $widget = shift;
    my ($start, $info);
    if($widget == $text){
        $start = '1.0';
        $info = $widget->Contents();
    }
    else {
        $start = '0.0';
        $info = $widget->get();
    }
    if($info =~ /^default$/){
        $widget->delete($start, 'end');
    }
}
于 2014-05-27T06:00:59.897 回答