检测鼠标离开 pygtk 窗口
在 PyGTK 应用程序中,我想检测鼠标指针何时离开我的顶级窗口。
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
...
window.connect("leave-notify-event", window_exit, "")
但是,只有当鼠标进入窗口内的小部件时才会触发该回调,而不是在离开顶级窗口时触发?
In PyGTK application, I'd like to detect when the mousepointer leaves my top-level window.
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
...
window.connect("leave-notify-event", window_exit, "")
However that callback is only triggered when the mouse enters a widget within the window, not when it leaves the top-level window?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是,当指针进入劣质小部件时,在 GTK 中,从技术上讲它也会离开窗口,这就是您遇到的奇怪行为的原因。
(顺便说一句。我完全没有使用 python 的经验,但我会尽力让它易于理解)
你的回调函数头应该看起来像这样:
事件非常重要,因为它的变量“event.detail”告诉我们到底是什么类型发生了请假事件。在您的情况下,您想要测试它是否等于“gtk.gdk.NOTIFY_NONLINEAR”,因为这意味着指针“真正”离开了窗口。
因此,您可能应该
在回调函数的顶部放置类似的内容。 (由于我不懂python,语法可能不完全正确)
Your problem is that, when the pointer enters an inferior widget, in GTK it is technically leaving the window as well, that's the reason for the weird behavior you're experiencing.
(Btw. I have absolutely no experience with python, but I'll try to make it understandable)
Your callback function head ought to look something like this:
The event is very important because its variable 'event.detail' tells us exactly what kind of leave-event happened. In your case, you want to test if it is equal to 'gtk.gdk.NOTIFY_NONLINEAR', because that means the pointer has "truly" left the window.
So, you should probably put something like
at the top of your callback function. (The syntax might not be exactly correct as I don't know python)