user494077
user494077

Reputation: 51

Get active gtk window in python

How would I get a handle to the active gtk.Window in python? (not a window I created, but the currently focused window).

Upvotes: 3

Views: 6083

Answers (2)

underscore_d
underscore_d

Reputation: 6795

[Note: This answers the question as the OP originally phrased it, which other readers will probably be searching for - not the very different question that they changed it to in comments on the other answer.]

If you have a GtkApplication and have added your GtkWindows to it - which you should probably do, because GtkApplication can do lots of really cool stuff! - then you can use GtkApplication's much simpler API dedicated to this purpose:

Gtk.Application.get_active_window():

Gets the “active” window for the application.

The active window is the one that was most recently focused (within the application). This window may not have the focus at the moment if another application has it — this is just the most recently-focused window within this application.

Upvotes: 1

ptomato
ptomato

Reputation: 57920

The answer is actually not OS-specific -- you can do it within GTK. You can get a list of all the toplevel windows from the application using gtk.window_list_toplevels(), then iterate through it until you find one where window.is_active() returns True.

If you want to consider other windows than the ones from your application, then you could try gtk.gdk.screen_get_default().get_toplevel_windows() but this will only get you GDK windows and not GTK windows, because you have no way of knowing whether those GDK windows are actually associated with GTK windows.

Upvotes: 5

Related Questions