The First Signal Handler

Now that we've set up our first signal in glade, lets define the handler for that signal in python.

Destroy Signal

Remember that our signal handler is named "on_app1_destroy", and that we are handling the destroy signal. PyGTK and Libglade, upon running our program, will recognize that it will set up the handler for the destroy signal for the widget app1 (glade's default name for a gnome window) and that we have associated the name "on_app1_destroy" for the signal handler. Now all we need to do is associate the name "on_app1_destroy" with a python function that will achieve the desired result.

Going back to the concepts of the delete and destroy signal quickly, remember that the window manager sends the delete signal to the widget that is being closed; if the handler returns gtk.TRUE (or any true value), it stops; if it returns false, the destroy signal is sent. The idea behind this is that a signal that is successfully handled will return a value, and a signal that is ignored (returns false) will set off the default sequence of events.

The default action for the delete signal then is to send the destroy signal, and the destroy signal automatically destroys the widget it's sending the signal to; in this case, the main window of our program. But the program hangs when we send the delete signal, because we're still caught up in the gtk.main() loop. To exit from this loop, we'll call gtk.main_quit(). The preliminary discussion on Signals and Events should explain the following new version of our simple program. Note that the signal_autoconnect function is actually a method of the widget tree class.

FILE: simple2.py -rwxr-xr-x [405b]


01: #!/usr/bin/env python
02: import gtk
03: import gnome.ui
04: import gtk.glade
05: 
06: # app1 destroy signal handler; exits the program
07: def destroy_signal_handler(arg):
08:     gtk.main_quit()
09: 
10: # necessary for gtk.glade.XML() with gnome widgets
11: gnome.init("app1", "1.0")
12: widgetTree = gtk.glade.XML("project1.glade")
13: 
14: destroy_dic = { "on_app1_destroy", destroy_signal_handler }
15: widgetTree.signal_autoconnect(destroy_dic)
16: 
17: gtk.main()

back to the Glade and Signals | forward to Help -> About Dialog
up to the index