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

Hey all, I've written a pretty simple program to do some registry editing. It has a section that allows you to open a .pm file which is a Gui I created. In that second Gui, I added a Close button with (command=>\$exit) therein. The rub is that when I close that gui via the button, the entire program closes down, rather than closing that window and defaulting to the original (still open) window. Is there a different set of exit codes I'm missing? Am I calling the .pl file incorrectly? ralfthewise

Replies are listed 'Best First'.
Re: Gui's being difficult
by stefan k (Curate) on Jun 07, 2001 at 11:56 UTC
    When you're doing it in perl-gtk it could be something like this:
    # ... my $dialog = new Gtk::Dialog; $dialog->set_title("About"); $dialog->set_modal($true); $dialog->set_position('center'); # ... fill dialog window with content # close button $button = new Gtk::Button( "OK" ); # the variable(s) after \&close_dialog get passed as args to the sub $button->signal_connect('clicked', \&close_dialog, $dialog); # ... show widgets # the close_dialog subroutine sub close_dialog { my ($button,$d) = @_; $d->destroy(); # such a global var might come in handy for some checks: $dialog_is_on = 0; }
    Of course the destroy()-Method works for other widgets, too. In your case it might be a Gtk::window

    Regards... Stefan

Re: Gui's being difficult
by the_slycer (Chaplain) on Jun 07, 2001 at 03:25 UTC
    exit is just that, it is designed to exit the program. What you need to look for is a subroutine to destroy the window. You don't mention what you are using for a GUI, but assuming it's Tk, something like command => $widget->destroy(); should work.

    Update of course, if you would like this to actually work you should probably use command => sub{ $widget->destroy() } as tye most kindly points out below. (Sorry, it's been many months since I last looked at Tk)

      Don't you mean command => sub { $widget->destroy() }? Otherwise the widget will be destroyed when you declare the button, rather than when you click it.

              - tye (but my friends call me "Tye")