问题
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 example.
Does anyone have one to share, please?
回答1:
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")
来源:https://stackoverflow.com/questions/2088451/gobject-add-emission-hook-usage