http://qs321.pair.com?node_id=292828

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

I just started teaching myself using Gtk-perl, Glade and glade2perl and have a simple problem that is probably stemming from my lack of understanding of how all the modules Glade produced interact or some dilusion I have with objects/classes.

I have 2 windows. The main one loads up, then I have a button that opens the second window (modal) using window2->run (is that the correct way to do it?)

If I then close that window using the 'X' (close icon) provided by the window manager it closes and all the relevant signals get generated.

I want to be able to close this second window via code, so that particular buttons in the window could also close it, but I can't figure out how!

I'd have thought I could call the destory or hide on the window or even emit a delete_event, but it seems from the errors I can't call/do any of these thing on the second window object because I am calling them from within the object and it is therefore not blessed.

Am I using all these modules Glade kindly produced for me incorrectly? or is there a way to call these methods on one's self as it seems that is what I need to do ... and specifically how to do all that and close the window?

Thanks, Ryan

Replies are listed 'Best First'.
Re: Closing GTK child windows via code
by thinker (Parson) on Sep 20, 2003 at 11:20 UTC

    Hi ryan,

    When you create the window, assign it to a variable, then call the hide() or destroy() method on the window. Use hide if you will be reusing the window.

    Here is some sample code which brings up a window with 3 buttons. The first brings up an (imaginary) error in a new window, the second closes the app, and the third closes the other window ( the one with the error ). There is also a button on the error window to close itself.

    Hope this helps

    thinker

      Thanks,

      I already understand and have this type of thing working. The problem arises because glade2perl makes each window into a package and it is invoked as such: window2->app_run which returned nothing to assign to a variable because it hands control to Gtk->main within the app_run sub.

      It looks like i was missing some of the default code that glade2perl puts at the top of each package. It puts a destroy_Form sub as well as toplevel_hide, toplevel_close and toplevel_destroy subs.

      Now that I have them in there I can call any of them and it works. The destroy_Form sub calls Gtk->main_quit. The other 3 subs call: shift->get_toplevel->hide/close/destroy, and still work even when I supply no arguments to them from within a button click handler in the same package.

      I hope this is what I'm supposed to be doing, at least it seems to work on the surface, who knows what it is doing or how much memory I'm chewing up needlessly behind the scenes :)

      I thought I was going batty having to try to call methods of an object from within that class when I had no reference to that object to base the call on! - I was oblivious to the fact that subroutines were specially made to do what I was trying to do and I simply didn't cut and paste them correctly from the glade2perl output :|

      Ryan *hides* :)