python - GObject.add_emission_hook only works once -
i want use gobject.add_emission_hook connect catch signal of instances of class. seems work, once. in minimal example below "signal received" printed once, no matter how many times 1 of buttons clicked. why , how can receive signal on every click?
of course in applications things more complicated , receiver (here class foo) not know objects emitting signals. therefore, connecting signals of objects not possible.
from gi.repository import gtk 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
event handler callbacks triggered on successive events. if return false
(when not returning anything, i'm guessing false
returned) hook removed. can illustrated following example based on sample:
from gi.repository import gtk 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 removed def on_button_rel(self, *args): print "release signal received" # result in warning gobject.remove_emission_hook(gtk.button, "button-press-event",self.hook_id) return true win = mywindow() foo = foo() gtk.main()
hope helps!
Comments
Post a Comment