Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks , I am new to this great Module "TK" . I am writting a small gui that recive user inputs. I am using the "->Entry()" function to reade the entry from the user. what I am doing is saving the entry after that. It is working fine, my question is how do you clear the entry window after you press the botton of save or ok . Ok let me phrase it again , I only wants the entries to get cleared after the button is pressed. Any idea? I hope I am making sence. thanks

Replies are listed 'Best First'.
Re: Pl/Tk
by graff (Chancellor) on Mar 01, 2004 at 06:51 UTC
    Here's a minimal example that shows one way to do it (probably the simplest way):
    #!/usr/bin/perl use strict; use Tk; my $mw = MainWindow->new(); my $txtvar; # have a separate scalar for each Entry widget $mw->Entry(-textvariable=>\$txtvar # this is the trick )->pack(); $mw->Button(-text=>"Print & Clear", -command=>sub{print $txtvar,$/; $txtvar=""} )->pack(); MainLoop;
    If the Entry widget is configured to have a "-textvariable" (a reference to a scalar), then the callback function on a button simply needs to reset that variable to an empty string. Naturally, if you have a lot of Entry widgets, it may be handy for their textvariables to be elements of an array or hash, in which case you give each Entry a reference to an element of the array or hash:
    my %strings = qw/first one second two third three/; for my $name ( sort keys %strings ) { $mw->Label(-text => $name)->pack(); $mw->Entry(-textvariable => \$strings{$name})->pack(); } $mw->Button(-text=>"Print&Clear", -command=>sub{ for (sort keys %strings) { print "$_=$strings{$_} "; $strings{$_} = ""; } print $/; } )->pack();
Re: Pl/Tk
by PodMaster (Abbot) on Mar 01, 2004 at 05:45 UTC
    `perldoc Tk::Entry'
    ...

    $entry->delete(first, ?last?)

    Delete one or more elements of the entry. First is the index of the first character to delete, and last is the index of the character just after the last one to delete. If last isn't specified it defaults to first+1, i.e. a single character is deleted. This method returns an empty string.

    ...

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Pl/Tk
by smackdab (Pilgrim) on Mar 01, 2004 at 05:41 UTC
    posting a code snipit make it easier to help ;-)

    Did you use the -textvariable option? It might make what you are trying to do easier...
      $entry->delete(0.0, 'end')
      How does the use of -textvariable make this easier? Am I missing something?
        I think it makes it make more "sense" to some programmers who might not know GUIs that well...
        I am sure just as many don't like the magic involved ;-)

        use Tk; my $v = 'one'; my $t = new MainWindow; my $e = $t->Entry(-textvariable=>\$v)->pack(); my $b = $t->Button(-text=>'Clear', -command=>sub{$v=""})->pack(); MainLoop();
        some people think $foo = ""; is easier than what you wrote, go figure