How can I list all open (X11) windows on Gnu/Linux from a Python script?
Asked Answered
H

1

5

I'd like to be able to grab a list of all windows that are open on a Linux desktop from a Python script. I suppose this would require working through Xlib or some other x11 or xdisplay library. This would be the Linux equivalent on win32's EnumWindows API call.

Ideally, I'd like to be able to use this to get a list of the title/caption text of every open window along with position/size information.

Is there some function call from Python that will return this info?

Helicograph answered 27/9, 2018 at 22:25 Comment(1)
Python XLib github.com/python-xlib/python-xlib and XQueryTree.Impressure
I
9

Install python-xlib:

pip3 install python-xlib

Try this:

from Xlib import display

d = display.Display()
root = d.screen().root

query = root.query_tree()

for c in query.children:
    # returns window name or None
    name = c.get_wm_name()
    if name: 
        print(name)

I'm not sure about the other properties. query.children is a list of Window objects, so some research on those should turn up something.

Window object docs.

Impressure answered 28/9, 2018 at 0:3 Comment(2)
Why some windows are missing?During
@During if you are using "Wayland" most of the packages can't get native windows.Intellectuality

© 2022 - 2024 — McMap. All rights reserved.