请参阅此代码示例:
import gtk
class MenuBox(gtk.EventBox):
def __init__(self):
super(MenuBox, self).__init__()
self.set_visible_window(False)
self.connect('enter-notify-event', self._on_mouse_enter)
self.connect('leave-notify-event', self._on_mouse_leave)
btn = gtk.Button('x')
btn.set_border_width(12)
self.add(btn)
def _on_mouse_enter(self, wid, event):
print '_on_mouse_enter'
def _on_mouse_leave(self, *args):
print '_on_mouse_leave'
def main():
win = gtk.Window()
win.connect('destroy', gtk.main_quit)
win.add(MenuBox())
win.show_all()
gtk.main()
if __name__ == '__main__':
main()
如果我从父母到孩子再返回,我希望不会触发进入和离开事件。我知道在这种特殊情况下,我可以使用event.detail
过滤这些事件。但如果没有边界,这不起作用。如果我删除边框,则根本不会触发事件。
在我的真实代码中,我有一个更复杂的小部件(基于gtk.Fixed),它在开头有边框但不在末尾。因此,将事件移动到孩子身上也不会有效。
答案 0 :(得分:1)
# self.set_visible_window(False)
self.connect('enter-notify-event', self._on_mouse_enter)
self.connect('leave-notify-event', self._on_mouse_leave)
btn = gtk.Button('x')
# btn.set_border_width(12)
这就是你需要的吗?