5 GObject signals are a system for registering callbacks for specific events.
7 To find all signals of a class you can use the
8 :func:`GObject.signal_list_names` function:
13 >>> GObject.signal_list_names(Gio.Application)
14 ('activate', 'startup', 'shutdown', 'open', 'command-line', 'handle-local-options')
18 To connect to a signal, use :meth:`GObject.Object.connect`:
22 >>> app = Gio.Application()
23 >>> def on_activate(instance):
24 ... print("Activated:", instance)
26 >>> app.connect("activate", on_activate)
29 ('Activated:', <Gio.Application object at 0x7f1bbb304320 (GApplication at 0x5630f1faf200)>)
33 It returns number which identifies the connection during its lifetime and which
34 can be used to modify the connection.
36 For example it can be used to temporarily ignore signal emissions using
37 :meth:`GObject.Object.handler_block`:
41 >>> app = Gio.Application(application_id="foo.bar")
42 >>> def on_change(*args):
45 >>> c = app.connect("notify::application-id", on_change)
46 >>> app.props.application_id = "foo.bar"
47 (<Gio.Application object at 0x7f1bbb304550 (GApplication at 0x5630f1faf2b0)>, <GParamString 'application-id'>)
48 >>> with app.handler_block(c):
49 ... app.props.application_id = "no.change"
51 >>> app.props.application_id = "change.again"
52 (<Gio.Application object at 0x7f1bbb304550 (GApplication at 0x5630f1faf2b0)>, <GParamString 'application-id'>)
56 You can define your own signals using the :obj:`GObject.Signal` decorator:
59 .. function:: GObject.Signal(name='', flags=GObject.SignalFlags.RUN_FIRST, \
60 return_type=None, arg_types=None, accumulator=None, accu_data=None)
62 :param str name: The signal name
63 :param GObject.SignalFlags flags: Signal flags
64 :param GObject.GType return_type: Return type
65 :param list arg_types: List of :class:`GObject.GType` argument types
66 :param accumulator: Accumulator function
67 :type accumulator: :obj:`GObject.SignalAccumulator`
68 :param object accu_data: User data for the accumulator
73 class MyClass(GObject.Object):
75 @GObject.Signal(flags=GObject.SignalFlags.RUN_LAST, return_type=bool,
77 accumulator=GObject.signal_accumulator_true_handled)
78 def test(self, *args):
79 print("Handler", args)
82 def noarg_signal(self):
87 def test_callback(inst, obj):
88 print "Handled", inst, obj
91 instance.connect("test", test_callback)
92 instance.emit("test", object())
94 instance.emit("noarg_signal")