GObject.add_emission_hook usage

怎甘沉沦 提交于 2020-01-06 06:56:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!