如何在Perl / Tk中按下按钮后清除GUI中的条目小部件

时间:2010-11-01 10:31:02

标签: perltk

use Tk;
$filename  = 'configuration.txt';

$mw = MainWindow->new;
$mw->geometry("500x250");
$f = $mw->Frame->pack(-side => 'bottom');

$f->Button(-text => "Exit",-command => sub { exit; })->pack(-side => 'left');
$f->Button(-text => "Save",-command => \&save_file)->pack(-side => 'left');

$t = $mw->Scrolled("Text", -width => 40, -wrap => 'none')->pack(-expand => 1, -fill => 'both');

foreach (qw/IP_ADDRESS_SS PORT_NUMBER_CLIENT PROTOCOL_CLIENT PORT_NUMBER_SERVER PROTOCOL_SERVER/)
{
                $w = $t->Label(-text => "$_:", -relief => 'groove', -width => 30);
                $t->windowCreate('end', -window => $w);
                $w = $t->Entry(-width => 20, -textvariable => \$info{$_});
                $t->windowCreate('end', -window => $w);
                $t->insert('end', "\n");
}
$t->configure(-state => 'disabled'); # disallows user typing
my $clear_text = $f->Button(-text => "Clear Text",-command => \&clear_entry)->pack(-side => 'left',
                                                                                  -anchor=>'se',
                                                                                  );

MainLoop;

##### Subroutine #####
sub save_file
{
    print"$filename\n";
    $info = "Saving '$filename'";
    open (FH, ">$filename");
    print FH $t->get("1.0", "end");
    $info = "Saved.";
}
sub clear_entry
{
  $w->delete('0', 'end');
}

这是使用标签和条目小部件进行简单数据输入的perk Tk程序

在这里,我想要的是如果单击明文按钮我要清除条目小部件中的所有条目, Plz帮助如何做到这一点

感谢你! Ranjith

1 个答案:

答案 0 :(得分:1)

一种方法是保存输入对象,以便在clear_entry()子例程中引用它们。

@entries;
foreach (qw/IP_ADDRESS_SS PORT_NUMBER_CLIENT PROTOCOL_CLIENT PORT_NUMBER_SERVER PROTOCOL_SERVER/) 
{ 
            $w = $t->Label(-text => "$_:", -relief => 'groove', -width => 30); 
            $t->windowCreate('end', -window => $w); 
            $w = $t->Entry(-width => 20, -textvariable => \$info{$_}); 
            $t->windowCreate('end', -window => $w); 
            $t->insert('end', "\n"); 
            push(@entries, $w);
} 
$t->configure(-state => 'disabled'); # disallows user typing 
my $clear_text = $f->Button(-text => "Clear Text",-command => \&clear_entry)->pack(-side => 'left', 
                                                                                  -anchor=>'se', 
                                                                                  ); 
MainLoop;


sub save_file { print"$filename\n"; $info = "Saving '$filename'"; open (FH, ">$filename"); print FH $t->get("1.0", "end"); $info = "Saved."; } 

sub clear_entry { 
    foreach $entry (@entries) {
        $entry->delete('0', 'end'); }
}