user1248084
user1248084

Reputation:

Getting the active root window using gtk.gdk in Python

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.

Upvotes: 2

Views: 3368

Answers (1)

elya5
elya5

Reputation: 2258

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).

Upvotes: 3

Related Questions