2012-07-07 3 views
1

GObject.add_emission_hook을 사용하여 클래스의 모든 인스턴스에 대한 신호를 수신하려고합니다. 그것은 일하는 것처럼 보이지만 한 번만. 아래의 최소 예에서 "신호 수신"은 버튼 중 하나를 클릭 한 횟수에 관계없이 한 번만 인쇄됩니다. 그 이유는 무엇이며 클릭 할 때마다 신호를받을 수 있습니까?GObject.add_emission_hook은 한 번만 작동합니다.

물론 내 응용 프로그램에서는 상황이 더 복잡하고 수신기 (여기 Foo 클래스)가 신호를 내보내는 객체를 알지 못합니다. 따라서 객체 자체의 신호에 연결할 수 없습니다. 콜백이 연속 이벤트를 트리거하는

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

답변

2

당신은 당신의 이벤트 핸들러에서 True를 반환해야합니다. False을 반환하면 (아무 것도 반환하지 않으면 False이 반환 된 것으로 추측됩니다) 후크가 제거됩니다. 이것은 귀하의 견본에 기초한 다음 예를 통해 설명 될 수 있습니다 :

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

희망이 있습니다.

+0

True를 반환하면 문제가 해결됩니다! gtk 문서에서 그런 것들을 찾는 것은 매우 어렵습니다. 감사! – uuazed

관련 문제