Search code examples
pythonxlib

Why do some (e.g Emacs and Chrome) X windows report no name and class?


When I use python-xlib to grab the currently focused window with get_input_focus(), the window name and class is set correctly for Konsole, but for Chrome and Emacs, they are just empty strings (although the window ID seems valid for all three). Why?

How can I get the title and owner process of these windows? Since I use KDE, using DBUS for these things is an option, but I would prefer a more general solution.


Solution

  • If found a similar question here: How do I detect the currently focused application?

    I modified it ever so slightly to this:

    cur_window = the_display.get_input_focus().focus
    cur_class = None
    while cur_class is None:
        cur_name = cur_window.get_wm_name()
        cur_class = cur_window.get_wm_class()
        if cur_class is None:
            cur_window = cur_window.query_tree().parent
    

    and now it works.