Update an entry in Perl Tk

651 Views Asked by At

I just start Perl Tk and I had a look on some tutorials but I have a problem. When I click on a button it displays on the entry widget the scalar that I want. It works but when I click an other time it keeps what was written on the entry. So I have two hello. I know that it comes from insert(0, "Hello") but I don't what to put instead of 0.

#!/usr/local/bin/perl
use Tk;

my $mw = MainWindow->new;
$mw->geometry("500x350+0+0");
$mw->title("Report Information about a Protein of Interest");

my ($bite) = $mw -> Label(-text=>"Enter the uniprot accession number:")->grid(-row => 0, - column => 0);
my ($ent) = $mw->Entry()->grid(-row => 0, - column => 1, -columnspan => 2, -sticky => 'nsew');
   $ent2 = $mw->Button(-text=> "Search", -command => \&push_button)->grid(-row => 1, - column => 0);

MainLoop;

#This is executed when the button is pressed
sub push_button {
    $ent -> insert(0,"Hello, ");
}
1

There are 1 best solutions below

0
Håkon Hægland On

The insert method for a Tk::Entry widget inserts text after the current insertion cursor position; to delete the existing text in the widget before inserting you can do:

sub push_button {    
    $ent -> delete(0, 'end');  # clears the widget
    $ent -> insert(0,"Hello, ");
}