Gtk.Builder, container subclass and child widget binding

I am trying to use custom container widgets in the gtk.Builder definition files. As for instantiating these 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, do I have a gtk.TreeView widget inside this dialog. I am trying to figure out how to bind this widget to an instance variable MyDialog.

One cheap alternative that I can think of is to call an additional method after getting a dialog widget:

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

But it seems rather uncomfortable. Has anyone decided this already or has a better idea on how to do this?

Thanks,

+4
source share
1 answer

Well, I probably answered my question.

One way to do this is to override gtk.Buildable parser_finished (), which gives access to the builder that created the class instance itself. The method is called after the entire XML file has been loaded, so all the additional widgets that we might want to get are already present and initialized:

 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 

It should be noted that for some reason (at least for me, in pygtk 2.12), if I do not explicitly inherit from gtk.Buildable, the override method is not called, even the thought of gtk.Dialog already implements the built-in interface.

+5
source

All Articles