Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Closing GTK child windows via code

by thinker (Parson)
on Sep 20, 2003 at 11:20 UTC ( #292843=note: print w/replies, xml ) Need Help??

in reply to Closing GTK child windows via code

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.

#!/usr/bin/perl -w use Gtk; use strict; my $dialog; my $label; my @errors; sub error{ if ($dialog){ $label->set_text( "ERROR " . $errors[rand @errors] ) }else{ $dialog = new Gtk::Window() ; my $bbox = new Gtk::Widget "Gtk::VBox", visible => 1, parent + => $dialog; my $button = new Gtk::Widget "Gtk::Button", label => "Close", +parent => $bbox, visible => 1; $label = new Gtk::Widget "Gtk::Label", label => $errors[rand +@errors], parent => $bbox, visible => 1 ; $button->signal_connect( "clicked", sub{ $dialog->hide }); } $dialog -> show; } sub my_quit { exit(0); } sub remote_quit{ $dialog -> hide } # or $dialog -> destroy; # if you w +ill not be opening it again init Gtk; my $window = new Gtk::Widget "Gtk::Window", type => -toplevel, title => "Error box", allow_grow => 0, allow_shrink => 0, border_width => 10, visible => 1; $window->signal_connect("destroy" => \&Gtk::main_quit); $window->signal_connect("delete_event" => \&Gtk::false); my $box1 = new Gtk::Widget "Gtk::VBox", homogeneous => 0, spacing => 5, parent => $window, visible => 1; my $bbox= new Gtk::Widget "Gtk::HButtonBox", spacing => 5, parent => $ +box1, visible => 1; my $fixed = new Gtk::Widget "Gtk::Fixed", visible => 1; $fixed->set_usize(200,50); $label = new Gtk::Widget "Gtk::Label", label => "Waiting on user input +", parent => $box1, visible => 1 ; new Gtk::Widget "Gtk::HSeparator", parent => $box1, visible => 1; my $err_btn = new Gtk::Widget "Gtk::Button", label => "Click me", clicked => "error", visible => 1, parent => $bbox; my $quit = new Gtk::Widget "Gtk::Button", label => "Quit", clicked => "my_quit", visible => 1, parent => $bbox; my $rquit = new Gtk::Widget "Gtk::Button", label => "Close the other window", clicked => "remote_quit", visible => 1, parent => $bbox; main Gtk; BEGIN{ @errors=( "Remote message: I don't like you. Go 'way.", "oh no, there is a 0 in the matrix!", "Error in matrix: Agent Smith Activated", "D'oh you've deleted the data set", "Matrix Error: You might want to take a pill", "so...lemme guess, you just clicked yes?", "no ks7 data left after removeal of zero channels", "Almost converged, you can probably stop now", "write(0,*) 'Game Over'", "I just Cannea do it Cap'n I just dinnea hea the powe +r.", "Warning: Can't get handle for AAAGH NO SWITCH IT OFF +!!! SWITCH IT OFFF!!!", "print *,'2.7182818'", "STOP PRESSING THE BUTTON GODDAMIT", + ); }

Hope this helps


Replies are listed 'Best First'.
Re: Closing GTK child windows via code
by ryan (Pilgrim) on Sep 20, 2003 at 12:01 UTC

    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* :)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://292843]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2021-09-28 03:40 GMT
Find Nodes?
    Voting Booth?

    No recent polls found