OJW
OJW

Reputation: 4612

Detect mouse leaving pygtk window

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?

Upvotes: 1

Views: 595

Answers (1)

Ancurio
Ancurio

Reputation: 1728

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:

def window_exit(widget, event, user_data)

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

if (event.detail != gtk.gdk.NOTIFY_NONLINEAR)  { return; }

at the top of your callback function. (The syntax might not be exactly correct as I don't know python)

Upvotes: 4

Related Questions