X11 - How to raise another application window using Python

I would like to be able to bring up another application window using Python.

I saw this, and I guess I can try:

X11: raise an existing window through the command line?

However, I would prefer to do this in Python, if at all possible.

+7
python x11
source share
3 answers

To activate another window, the right thing at the Xlib protocol level is to send the _NET_ACTIVE_WINDOW message as described in the EWMH specification http://standards.freedesktop.org/wm-spec/wm-spec-1.3.html

This can be done using python-xlib (presumably) or using gdk_window_focus () on someone else's GdkWindow using GDK via pygtk

_NET_ACTIVE_WINDOW is superior to XRaiseWindow () and has been in all important WMs for many years.

You should avoid XSetInputFocus (), which will cause problems (especially if you made a mistake in the timestamp). The problem is that WM cannot intercept SetInputFocus (), so it causes strange race conditions and user interface mismatches.

Indeed, only _NET_ACTIVE_WINDOW works correctly, so it was invented because previous hacks were bad.

There is a library called libwnck that allows you to activate windows (by the way), but unfortunately it adds a lot of overhead because it always keeps track of all open windows from any application, even if you don’t need to do this, however, if you want to track windows from other applications anyway, then libwnck has a function to activate those windows that do the right thing, and that would be a good choice.

A strictly correct approach is to check EWMH support _NET_ACTIVE_WINDOW (EWMH documents how to do it) and return to XRaiseWindow if WM does not have _NET_ACTIVE_WINDOW. However, since any WM that has been active over the past many years has EWMH, many people are lazy to cancel for legacy WM servers.

+5
source share

You need to use python-xlib and call .circulate(Xlib.X.RaiseLowest) on the window object (which can be identified in many, different ways - cannot figure out which one is right for you from zero amount of information about it in your Q; -). For an excellent example of using python-xlib check out tinywm window manager - after version C, the author gives the Python version, which takes about 30 non-empty lines without comments (for a convenient, if tiny, window manager ...! -).

+4
source share

You can see the python ewmh package . The documentation contains examples, but here is how you can achieve what you want:

 from ewmh import EWMH import random ewmh = EWMH() # get every displayed windows wins = ewmh.getClientList() # let active one window randomly ewmh.setActiveWindow(random.choice(wins)) # flush requests - that actually do the real job ewmh.display.flush() 
+4
source share

All Articles