diff options
author | Simon Feltman <s.feltman@gmail.com> | 2012-03-20 04:33:50 -0700 |
---|---|---|
committer | Martin Pitt <martinpitt@gnome.org> | 2012-09-03 15:45:20 +0200 |
commit | a7c524219987fbf37e455a91e4c78d2b9b4db12d (patch) | |
tree | 2bac1b63bcfa36f83bf6cf91665e9bb3290334b7 /examples | |
parent | 96fa22369fd188465559fc904c7f76e73040e6dd (diff) | |
download | pygobject-a7c524219987fbf37e455a91e4c78d2b9b4db12d.tar.gz |
[API add] Add Signal class for adding and connecting custom signals.
The Signal class provides easy creation of signals and removes the
need for __gsignals__ in client code. The Signal class can also be
used as a decorator for wrapping up the custom closure. As well as
providing a "BoundSignal" when accessed on an instance for making
connections without specifying a signal name string.
Python3 annotations can also be used to supply closure argument and
return types when Signal is used as a decorator. For example:
class Eggs(GObject.GObject):
@GObject.Signal
def spam(self, count:int):
pass
https://bugzilla.gnome.org/show_bug.cgi?id=434924
Diffstat (limited to 'examples')
-rw-r--r-- | examples/signal.py | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/examples/signal.py b/examples/signal.py index 9a9d00cc..69c1d624 100644 --- a/examples/signal.py +++ b/examples/signal.py @@ -1,28 +1,42 @@ +from __future__ import print_function + from gi.repository import GObject class C(GObject.GObject): - __gsignals__ = { - 'my_signal': (GObject.SIGNAL_RUN_FIRST, GObject.TYPE_NONE, - (GObject.TYPE_INT,)) - } + @GObject.Signal(arg_types=(int,)) + def my_signal(self, arg): + """Decorator style signal which uses the method name as signal name and + the method as the closure. - def do_my_signal(self, arg): - print "C: class closure for `my_signal' called with argument", arg + Note that with python3 annotations can be used for argument types as follows: + @GObject.Signal + def my_signal(self, arg:int): + pass + """ + print("C: class closure for `my_signal' called with argument", arg) + + @GObject.Signal + def noarg_signal(self): + """Decoration of a signal using all defaults and no arguments.""" + print("C: class closure for `noarg_signal' called") class D(C): def do_my_signal(self, arg): - print "D: class closure for `my_signal' called. Chaining up to C" - C.do_my_signal(self, arg) + print("D: class closure for `my_signal' called. Chaining up to C") + C.my_signal(self, arg) + +def my_signal_handler(obj, arg, *extra): + print("handler for `my_signal' called with argument", arg, "and extra args", extra) -def my_signal_handler(object, arg, *extra): - print "handler for `my_signal' called with argument", arg, "and extra args", extra inst = C() inst2 = D() inst.connect("my_signal", my_signal_handler, 1, 2, 3) +inst.connect("noarg_signal", my_signal_handler, 1, 2, 3) inst.emit("my_signal", 42) +inst.emit("noarg_signal") inst2.emit("my_signal", 42) |