FAQ Index - Search - Recent Changes - Everything - Add entry

<< Previous Entry | FAQ Entry 22.4 | Next Entry >>

22.4. I'm using libglade, but what do I do with the signals and handlers I set in the glade file?

Typically, you create an python object to represent a window, and then construct that object from a libglade file. PyGTK allows you to connect the signal handlers from the glade file directly to methods on your glade proxy object:

 class MyWindow:

    def __init__(self):
	# Load the glade file
        wtree = glade.XML('foo.glade')
	# Connect the glade signals handlers to the python callbacks
	wtree.signal_autoconnect(self)

    # define a callback (method)
    def a_name_of_a_signal_handler(self, *args):
        ....
The nice thing about autoconnecting to an object is that you connect to bound methods, so you always receive a reference to the object itself in the method arguments. The other arguments of the method are the standard signal arguments. Note that PyGTK _will not_ warn you about signal handlers that were registered in the glade file, but are not implemented in your object.

Often, you will also want to reference the widgets that you construct. This snippet of code, to be placed at the end of __init__, will make add each widget as an attribute of your object:

        for w in wtree.get_widget_prefix(''):
            name = w.get_name()
            # make sure we don't clobber existing attributes
            assert not hasattr(self, name)
            setattr(self, name, w)
The object-autoconnection feature was implemented in PyGTK 1.99.15; older versions don't have it. Other methods for doing the autoconnection follow.

Instead of passing signal_autoconnect an object, you can pass it a dict filled with methods, like

 dict[glade_method name] = python_method_reference
and call:

 gladetree.signal_autoconnect(dict)
for signal connection to be performed.

There are a number of base classes that wrap this process, as well. One is included with Kiwi: [www.async.com.br] and another, with Mitch Chapman's GladeBase: [ftp.ssc.com] (article at [www.linuxjournal.com] )

Jonathan Bartlett also contributed this class to the list:

 import new, types

 class GladeWidget:
    #This method loads the XML file and autoconnects the signals
    def initialize(self, glade_file, widget_name):

        #initialize variables
        self.widgets = GladeXML(glade_file, widget_name)
        callbacks = {}

        #find and store methods as bound callbacks
        class_methods = self.__class__.__dict__
        for method_name in class_methods.keys():
            method = class_methods[method_name]
            if type(method) == types.FunctionType:
                callbacks[method_name] = new.instancemethod(
                                         method, self, self.__class__)
        self.widgets.signal_autoconnect(callbacks)
Simply use this as the base class of your GLADE window, and just define the signal handlers as regular methods. In your __init__ method, you need to call self.initialize("yourfile.glade", "YourGLADEWindowName").

Here's an example where arguments other than the widget are passed to the signal handler (note the tuple for handling clicks to the ok button):

 wTree2 = libglade.GladeXML("somefile.glade","proxy1")
 proxywidget = wTree2.get_widget("proxy1")
 id=1
 dic= {"on_cancel_clicked": proxywidget.destroy,
       "gtk_widget_destroy": proxywidget.destroy,
       "on_ok_clicked": ( handle_ok_clicked, wTree2,id)}
 wTree2.signal_autoconnect (dic)
Then the handler would be defined like so:
 def handle_ok_clicked(self,widget,name,wTree2,widgetid):
Last but not least read this tutorial: [patrick.wagstrom.net]

PyGTK FAQ Wizard | PyGTK Homepage | Feedback to faq at pygtk.org