Get active gtk window in python

How do I get the active descriptor gtk.Windowin python? (not the window that I created, but the currently focused window).

+3
source share
2 answers

The answer is actually OS independent - you can do it in GTK. You can get a list of all the top-level windows from the application using gtk.window_list_toplevels(), and then iterate over it until you find the one where it window.is_active()returns True.

If you want to view other windows than those from your application, then you can try gtk.gdk.screen_get_default().get_toplevel_windows(), but this will only result in GDK windows, not GTK windows, because you have no way of knowing if these GDK windows are really associated with GTK windows.

+5
source

[Note: this answers the question in the form in which the OP formulated it that other readers are likely to look for - not the very question for which they changed it in the comments to another answer.]

If you have GtkApplicationand you have added your own to it GtkWindow- which you probably should do, because it GtkApplicationcan do a lot of really cool things! - then you can use a GtkApplicationmuch simpler API designed for this purpose:

Gtk.Application.get_active_window():

"" .

- , ( ). , - .

0

Source: https://habr.com/ru/post/1774546/


All Articles