views:

898

answers:

1

I'm trying to use custom container widgets in gtk.Builder definition files. As far as instantiating those widgets, it works great:

#!/usr/bin/env python

import sys 
import gtk 

class MyDialog(gtk.Dialog):
    __gtype_name__ = "MyDialog"


if __name__ == "__main__":
    builder = gtk.Builder()
    builder.add_from_file("mydialog.glade")

    dialog = builder.get_object("mydialog-instance")
    dialog.run()

Now the question is that say I have a gtk.TreeView widget inside that dialog. I'm trying to figure out how to bind that widget to an MyDialog instance variable.

One cheap alternative I can think of is to call additional method after getting the dialog widget like so:

dialog = builder.get_object("mydialog-instance")
dialog.bind_widgets(builder)

But that seems fairly awkward. Has anyone solved this already or has a better idea on how to go about doing it?

Thanks,

+4  A: 

Alright, I guess I answered my own question.

One way to do the above is to override gtk.Buildable's parser_finished(), which gives access to the builder object that created the class instance itself. The method is called after entire .xml file has been loaded, so all of the additional widgets we may want to get hold of are already present and intialized:

class MyDialog(gtk.Dialog, gtk.Buildable):
    __gtype_name__ = "MyDialog"

    def do_parser_finished(self, builder):
        self.treeview = builder.get_object("treeview1")
        # Do any other associated post-initialization

One thing to note is that for some reason (at least for me, in pygtk 2.12), if I don't explicitly inherit from gtk.Buildable, the override method doesn't get called, even thought gtk.Dialog already implements the buildable interface.

Dimitri Tcaciuc
+1 This is great! I understand now what you were trying to do.
Ali A