2

In Python, how would I get the active root window (or screen if you will) using gtk.gdk? I'm looking for a cross-platform solution.

The below code returns all my three monitors

gtk.gdk.get_default_root_window()

By active root window, I'd be happy with either the one the cursor is currently on, or the one the focused window is on.

Potentially helpful links:

[1] python pygtk how to put a window on a specific display monitor

EDIT: I attempted to implement the solution described in link #1, however screen.get_active_window() returns None and not an instance of gtk.gdk.Window.

Community
  • 1
  • 1

1 Answers1

3

This should get you the active window:

from gi.repository import Gdk
active_window = Gdk.get_default_root_window().get_screen().get_active_window()

or if you are using pygtk

from gtk import gdk
active_window = gdk.get_default_root_window().get_screen().get_active_window()

The active_window is a Gdk.Window (gtk.gdk.Window for pygtk).

elya5
  • 2,236
  • 1
  • 11
  • 27
  • Like I said in my main post `get_active_window()` return `None`. I suspect that it's because I'm on Windows as it seems to work on my Lubuntu laptop. I need a cross-platform solution. –  Mar 31 '15 at 14:52
  • 3
    I am afraid I cannot help you with a solution that also works on Windows. You should also consider to mention in your question that you are looking for a cross-platform solution. – elya5 Mar 31 '15 at 15:30
  • Edited my question to include cross-platform. –  Mar 31 '15 at 15:35
  • @Locercus it's a bit late, but I have a feeling there is no root window per monitor on Windows, just [one big root window (called the "desktop window")](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633504%28v=vs.85%29.aspx). I'm not 100% sure though. You can poke around the [multi-monitor API documentation on MSDN](https://msdn.microsoft.com/en-us/library/dd145071%28v=vs.85%29.aspx) if you'd like. A Windows API expert will need to confirm all this for me... – andlabs Apr 07 '15 at 04:08