Label does not trigger mouse event

前端 未结 2 721
野性不改
野性不改 2021-01-21 06:00

In the pygtk reference it states that every Gtk.Widget has the event enter-event-notify, but with my test code that event is never fired for the Label widget (with

2条回答
  •  梦毁少年i
    2021-01-21 06:35

    OK I've got the solution, it's the same as over here: Enter-Notify-Event Signal not working on gtk.ToolButton. For some non-obvious reason some widgets can't respond to signals on their own and need an extra box around them. I've rewritten the code example in a way I would have done it - namely using imports for more recent GTK 3.0 and a more object oriented style by deriving from Gtk.Window. Also one might prefer instance methods instead of nested ones.

    from gi.repository import Gtk
    
    class Foo (Gtk.Window):
    
        def __init__(self):
            Gtk.Window.__init__(self)
            self.connect("destroy", Gtk.main_quit)
            self.set_border_width(50)
            box = Gtk.EventBox()
            label = Gtk.Label("Test")   
            box.add(label)
            box.connect("enter-notify-event", self.on_mouse)
            self.add(box)
            self.show_all()
    
        def on_mouse(self, widget, data=None):
            print widget, data
    
        def main(self):
            Gtk.main()
    
    if __name__ == "__main__":
        Foo().main()
    

提交回复
热议问题