GObject.add_emission_hook只能使用一次 [英] GObject.add_emission_hook only works once

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

问题描述

我想使用GObject.add_emission_hook连接来捕获一个类的所有实例的信号。它似乎工作,但只有一次。在下面的最小例子中,接收到的信号只打印一次,而不管其中一个按钮被点击多少次。为什么会这样?我怎样才能在每次点击时收到一个信号?

当然,在我的应用程序中,事情更复杂,接收器(这里是Foo类)不知道发射信号的对象。因此,连接到对象本身的信号是不可能的。

  from gi.repository import gtk 
from gi .repository import GObject

class MyWindow(Gtk.Window):
$ b $ def __init __(self):
Gtk.Window .__ init __(self,title =Hello World)
vbox = Gtk.VBox()
self.add(vbox)
button = Gtk.Button(label =Click Here)
vbox.pack_start(button ,True,True,0)
button = Gtk.Button(label =Or There)
vbox.pack_start(button,True,True,0)
self.show_all()

class Foo:

def __init __(self):
GObject.add_emission_hook(Gtk.Button,clicked,self.on_button_press)

def on_button_press(self,* args):
printsignal received


win = MyWindow()
foo = Foo()
Gtk.main()


解决方案

您应该返回从您的事件处理函数中为回调触发连续的事件。如果你返回 False (当你没有返回任何东西时,我猜测 False 被返回),那么钩子是除去。这可以根据您的示例通过以下示例来说明:

  from gi.repository从gi导入Gtk 
。资源库导入GObject

class MyWindow(Gtk.Window):
$ b $ def __init __(self):
Gtk.Window .__ init __(self,title =Hello World )
vbox = Gtk.VBox()
self.add(vbox)
self.connect(destroy,lambda x:Gtk.main_quit())
button = Gtk.Button(label =Click Here)
vbox.pack_start(button,True,True,0)
button = Gtk.Button(label =Or There)
vbox。 pack_start(button,True,True,0)
self.show_all()

class Foo:
def __init __(self):
self.hook_id = GObject。 add_emission_hook(Gtk.Button,button-press-event,self.on_button_press)
GObject.add_emission_hook(Gtk.Button,button-release-event,self.on_button_rel)

def on_button_press(self,* args):
print按下标志al收到
返回False#钩子被删除

def on_button_rel(self,* args):
print收到释放信号
#这将导致警告
GObject.remove_emission_hook(Gtk.Button,button-press-event,self.hook_id)
返回True


win = MyWindow()
foo = Foo()
Gtk.main()

希望这有助于您! / p>

I want to use GObject.add_emission_hook to connect to catch a signal of all instances of a class. It seems to work, but only once. In the minimal example below "signal received" is only printed once, no matter how many times one of the buttons is clicked. Why is that and how can I receive a signal on every click?

Of course in my applications things are more complicated and the receiver (here the class Foo) does not know the objects emitting the signals. Therefore, connecting to the signals of the objects themselves is not possible.

from gi.repository import Gtk
from gi.repository import GObject

class MyWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Hello World")
        vbox = Gtk.VBox()
        self.add(vbox)
        button = Gtk.Button(label="Click Here")
        vbox.pack_start(button, True, True, 0)
        button = Gtk.Button(label="Or There")
        vbox.pack_start(button, True, True, 0)
        self.show_all()

class Foo:

    def __init__(self):
        GObject.add_emission_hook(Gtk.Button, "clicked", self.on_button_press)

    def on_button_press(self, *args):
        print "signal received"


win = MyWindow()
foo = Foo()
Gtk.main()

解决方案

You should return True from your event handler for callbacks to be triggered on successive events. If you return False (when you are not returning anything, I'm guessing False is returned) then the hook is removed. This can be illustrated with the following example based on your sample:

from gi.repository import Gtk
from gi.repository import GObject

class MyWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Hello World")
        vbox = Gtk.VBox()
        self.add(vbox)
        self.connect("destroy", lambda x: Gtk.main_quit())
        button = Gtk.Button(label="Click Here")
        vbox.pack_start(button, True, True, 0)
        button = Gtk.Button(label="Or There")
        vbox.pack_start(button, True, True, 0)
        self.show_all()

class Foo:
    def __init__(self):
        self.hook_id = GObject.add_emission_hook(Gtk.Button, "button-press-event", self.on_button_press)
        GObject.add_emission_hook(Gtk.Button, "button-release-event", self.on_button_rel)

    def on_button_press(self, *args):
        print "Press signal received"
        return False # Hook is removed

    def on_button_rel(self, *args):
        print "Release signal received"
        # This will result in a warning
        GObject.remove_emission_hook(Gtk.Button, "button-press-event",self.hook_id)
        return True


win = MyWindow()
foo = Foo()
Gtk.main()

Hope this helps!

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆