GObject.add_emission_hook usage
I was kindly directed to use GObject's "add_emission_hook" following a recent question on SO but I can't seem to find a usage ex开发者_Python百科ample.
Does anyone have one to share, please?
After a discussion with helpful folks on IRC #pygtk, here is what I came up with:
import gobject
class Signals(gobject.GObject):
__gsignals__ = {
"lastfm_username_changed": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, (gobject.TYPE_STRING,)) #@UndefinedVariable
}
def __init__(self):
gobject.GObject.__init__(self)
class Bus(object):
"""
Message Bus - Borg pattern
"""
_signals=Signals()
@classmethod
def emit(cls, name, *pa, **kwa):
cls._signals.emit(name, *pa, **kwa)
@classmethod
def add_emission_hook(cls, name, callback):
gobject.add_emission_hook(cls._signals, name, callback)
if __name__=="__main__":
def callback(self, data):
print "callback: data=%s" % data
Bus.add_emission_hook("lastfm_username_changed", callback)
Bus.emit("lastfm_username_changed", "jldupont")
精彩评论