How to clear the entry widget in a GUI after a button is pressed in Perl/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
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'); }
}
精彩评论