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

Hey all,
I've got a GUI that has 5 different entry boxes and a save button at the bottom that takes those entries and writes them to a file.
The rub is that I need to have the gui refresh after the user presses the save button. So basically I want the entry boxes to clear and be ready for another set of input after that save button is pressed, so that the user can continue to enter information, without having to delete the old stuff from the entry boxes on their own.
thanks,
John

Replies are listed 'Best First'.
(ichimunki) Re: Clearing entry boxes in a GUI?
by ichimunki (Priest) on Jun 21, 2001 at 20:43 UTC
    For Tk:
    #!/usr/bin/perl -w use strict; use Tk; my $Main = MainWindow->new(); my $text = "foobar"; my $Entry = $Main-> Entry( -textvariable => \$text )-> pack( -expand => 1, -fill => 'x' ); my $Button = $Main-> Button( -text => 'Clear', -width => 20, #choose one of the following #-command => sub { $text = '' } #-command => sub { $Entry->delete( 0, 'end' ) } )->pack(); MainLoop;
      Thanks!
      Another question if I may
      I have five entry boxes to clear. I used your second -command structure ($Entry->delete(0,'end') and chained it into the code like thus:
      $top->Button(-text=>"Save RegEdits to a File", -foreground=>"blue", -command=>sub{save()},
      -command => sub { $entry1->delete( 0, 'end' )},
      -command => sub { $entry1->delete( 0, 'end' )},
      -command => sub { $entry3->delete( 0, 'end' )},
      -command => sub { $entry4->delete( 0, 'end' )} )->pack()
      But the daisy chained -commands looking at each entry didn't all work. In fact, only the first worked, and for some reason the save fxn at the beginning didn't work either. What am I doing wrong?
      john
        You can only have one -command attribute per Button widget. You might try:
        -command => sub {$e1->delete(0,'end'); $e2->delete(0,'end'); ... $e5->delete(0,'end'); }
        Or better yet, instead of sub, use a reference to an actual subroutine so your button declaration isn't so messy and that way you can call the resulting sub from other places in the script if needed.
Re: Clearing entry boxes in a GUI?
by stefan k (Curate) on Jun 21, 2001 at 20:23 UTC
    Hi,
    you should mention what kind of GUI you got. I assume it is either Tk or Gtk. If it's Tk I can't be of any help to you.

    If it's Gtk you could just delete the text of the entry-widgets in the function which is connected to the save button.

    Say you got:

    # ... lots of stuff $entry = new Gtk::Entry(8); $button = new Gtk::Button("Save"); # pass the entry-varible to the sub, if it ain't global $button->signal_connect( "clicked", \&savethings, $entry); # ... lots of more stuff :-)
    The the function savethings could be something like
    sub savethings { # here we receive the entry-widget (and the save-button, too) my ($savebutton,$entry) = @_; # delete text in entry $entry->set_text(""); # save things # ... }

    Hope this helps.

    Regards... Stefan

Re: Clearing entry boxes in a GUI?
by Zaxo (Archbishop) on Jun 21, 2001 at 20:30 UTC

    It would be helpful for you to post the code in question. It sounds like you have those entries in global variables. If true, you should change that.

    Try making the default values part of the dialog box constructor, as arguments to it perhaps.

    After Compline,
    Zaxo