Gtk.Template init_template function does not no-op if called manually
I believe this is a bug.
Looking at this line, it seems as if the intended functionality is to make
init_template perform a no-op if called twice so that the user can call it from their
__init__ if they wish. This does not appear to be working as intended
More specifically, suppose I define a widget like so:
TEMPLATE_WIDGET = """ <interface> <template class="TestObject" parent="GtkBox"> <child> <object class="GtkLabel" id="example_label" /> </child> </template> </interface> """ @Gtk.Template.from_string(TEMPLATE_WIDGET) class TestObject(Gtk.Box): __gtype_name__ = 'TestObject' example_label = Gtk.Template.Child() def __init__(self): super(Gtk.Box, self).__init__() label_is_bound = isinstance(self.example_label, Gtk.Label) print('widget is bound?: %s' % label_is_bound) if not label_is_bound: self.init_template() self.example_label.set_label('my label')
I've noticed that when I directly construct a new
TestObject, that the child widgets are bound in the
init code, e.g.
test = TestObject() # prints 'widget is bound?: True'
But when I construct a
TestObject indirectly by using it in another template, the child widgets are not bound in the
init code, e.g.
USAGE_EXAMPLE = """ <interface> <object class="GtkBox"> <child> <object class="TestObject"/> </child> </object> </interface> """ # prints 'widget is bound?: False' Gtk.Builder.new_from_string(EXAMPLE, len(EXAMPLE))
By itself, this is a bit confusing, but I guess it's not a bug per se.
The bug is that when I explicitly call the
init_template line, it ends up binding the widgets twice. i.e. by the time the UI appears, this line has executed twice on the same object which causes all sorts of problems.
It appears that the no-op line linked above is not functioning as intended. I'm not sure why.
It's important that I have access to the child widgets in
__init__ so I can perform some complicated logic when the object is initialized.