1

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?

OJW
  • 4,514
  • 6
  • 40
  • 48

1 Answers1

4

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)

Ancurio
  • 1,698
  • 1
  • 17
  • 32
  • OK, I'd have no problem ignoring the cases when it goes into an "inferior widget". However, the callback function isn't gettting called at all when the mouse leaves the top-level window (in Windows XP at least) – OJW Jan 10 '12 at 15:52
  • @OJW Oh, I'm sorry, this worked for me on Linux.. maybe it's a bug then? – Ancurio Jan 10 '12 at 18:09