Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Register
  • Sign in
  • pygobject pygobject
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 302
    • Issues 302
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 31
    • Merge requests 31
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Container Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • GNOMEGNOME
  • pygobjectpygobject
  • Issues
  • #386
Closed
Open
Issue created Feb 27, 2020 by Samuel Bryant@sbryant1014

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.

Assignee
Assign to
Time tracking