Getting notification for GObject with custom param

Hey folks,

I’m exploring properties and signals in Python, following from the documentation:

https://pygobject.gnome.org/guide/api/properties.html
https://pygobject.gnome.org/guide/api/signals.html

I’d like to create Gtk.Template classes with custom parameters. It wasn’t working so I made a very simple test case:

from gi.repository import GObject


class Meaning(GObject.Object):
    def __init__(self):
        super().__init__()
        self._meaning_of_life = 42

    @GObject.Property(type=int)
    def meaning_of_life(self):
        return self._meaning_of_life

    @meaning_of_life.setter
    def meaning_of_life(self, new_meaning):
        self._meaning_of_life = new_meaning


def meaning_fn(instance, param):
    print("The meaning of life has been redefined.")


meaning = Meaning()
meaning.set_property("meaning_of_life", 43)
meaning.connect("notify::meaning_of_life", meaning_fn)

My meaning_fn() callback isn’t called. Why is that?

Thanks again,

J.R.

You’re connecting to the signal after setting the property. Signals are synchronous, and so are property accessors.

Additionally, you’re using the wrong detail for the notify signal: the detail must be the canonical property name. See:

If you change the main section with this:

meaning = Meaning()
meaning.connect("notify::meaning-of-life", meaning_fn)
meaning.set_property("meaning-of-life", 43)

You’ll see the correct result on your terminal:

❯ python3 ./property-notify.py
The meaning of life has been redefined.

Aha!

In my defense, the order of set_property() and connect() was a cut-and-paste error from my unit tests.

The canonical name was the problem.

Thanks again!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.