本文介绍了GObject.add_emission_hook用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被指示使用最近关于SO的问题之后,GObject的"add_emission_hook" 但我似乎找不到使用示例.

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.

请问有人可以分享吗?

推荐答案

在IRC #pygtk上与乐于助人的人们讨论之后,这是我想出的:

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")

这篇关于GObject.add_emission_hook用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:43