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

发布于 2024-09-30 05:59:57 字数 1665 浏览 1 评论 0原文

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 程序

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

谢谢你! 兰吉斯

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

This is the perk Tk program for simple data entry using label and entry widgets

Here,what i want is if click the clear text button i want to clear all the entry in the entry widget,
Plz help how to do this

Thanking u !
Ranjith

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感悟人生的甜 2024-10-07 05:59:57

一种方法是保存条目对象,以便您可以在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'); }
}

One way is to save off the entry objects so you can reference them in the clear_entry() subroutine.

@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'); }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文