问题
How would I get a handle to the active gtk.Window
in python? (not a window I created, but the currently focused window).
回答1:
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.
回答2:
[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 GtkWindow
s 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.
来源:https://stackoverflow.com/questions/4166169/get-active-gtk-window-in-python