3

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

4
  • I suppose it breaks down to two questions. 1) What window is active? 2) Is this window a gtk window? What OS are you using? Any answer will be OS specific. Commented Nov 12, 2010 at 15:39
  • Hi Steven. Thanks for the comment. I am working with ubuntu linux. Commented Nov 12, 2010 at 22:48
  • I don't know if it helps but on Ubuntu you can get the active window ID with import subprocess; subprocess.Popen(['/bin/bash', '-c', 'xdotool getactivewindow']) Commented Jul 6, 2014 at 20:04
  • This is a question that needs to be answered on the level of the window manager or compositor, not GTK+; the widget toolkit only works within a single process (or GtkApplication, of which there apparently can be >1 per process, though I'm not sure anyone has ever found any use for that). Commented Jul 11, 2017 at 15:22

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

7 Comments

This is not what I want - window.is_active() returns whether a given window is active, I need the actual window object. I have a handle to the wnck window or to the gtk.gdk.Window, but I need the gtk.Window object.
The second paragraph is exactly my problem - I have a list of gdk windows (I can also use wnck and get a similar list of wnck windows) but I need the gtk windows. My real effort is to determine what type of widget inside the window has user focus (e.g. text-edit).
There's no way of knowing that - perhaps the focused widget is some custom widget that your program doesn't even know about! I don't think what you want is possible.
gtk.window_list_toplevels() returns as [] for me. I"m on Ubuntu with plenty of windows open,
Did you read the answer? That won't give you windows from other applications.
|
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.