from gi.repository import Gtk, GObject def on_button_clicked(button): print("Button clicked") button = Gtk.Button(label="Click me") def hook(*args): print("Signal emitted") button.connect("clicked", on_button_clicked) # Adding the emission hook button.add_emission_hook("clicked", hook) # Displaying the button win = Gtk.Window() win.add(button) win.show_all() Gtk.main()
from gi.repository import GObject class MyObject(GObject.GObject): __gproperties__ = { "value": (int, "Value", "The value property", 0, 100, 0, GObject.PARAM_READWRITE) } def __init__(self): super().__init__() self._value = 0 def do_get_property(self, prop): if prop.name == "value": return self._value else: raise AttributeError("unknown property {}".format(prop.name)) def do_set_property(self, prop, value): if prop.name == "value": self._value = value else: raise AttributeError("unknown property {}".format(prop.name)) obj = MyObject() def hook(*args): print("Signal emitted") # Adding the emission hook obj.add_emission_hook("notify::value", hook) # Changing the value property obj.set_property("value", 10) # Getting the value property value = obj.get_property("value") print("The value is: {}".format(value))This example creates a custom GObject-type class with a "value" property. A hook is added to detect when the "notify::value" signal is emitted, and the property value is then set and retrieved to demonstrate the hook in action. The `add_emission_hook` method is part of the GObject module in the PyGObject package.