How to get an Xlib.display.Window instance by id?
Asked Answered
F

1

7

I have found the following code (http://pastebin.com/rNkUj5V8), but I would rather use a direct lookup:

import Xlib
import Xlib.display

def get_window_by_id(winid):
    mydisplay = Xlib.display.Display()
    root = mydisplay.screen().root # should loop over all screens
    inspection_list = [root]

    while len(inspection_list) != 0:
        awin = inspection_list.pop(0)
        if awin.id == winid:
            return awin
        children = awin.query_tree().children
        if children != None:
            inspection_list += children

    return None

# use xwininfo -tree to click on something (panel was good for me)
# until you find a window with a name, then put that id in here
print get_window_by_id(0x1400003)
print get_window_by_id(0x1400003).get_wm_name()

I have tried instantiating a Window object directly, but then the call to get_attributes fails:

w = Xlib.xobject.drawable.Window(Xlib.display.Display(), 67142278)
w.get_attributes()

/usr/lib/python2.7/dist-packages/Xlib/display.pyc in __getattr__(self, attr)
    211             return types.MethodType(function, self)
    212         except KeyError:
--> 213             raise AttributeError(attr)
    214 
    215     ###

AttributeError: send_request
Floristic answered 25/2, 2014 at 12:53 Comment(0)
U
11

Use dpy.create_resource_object('window', 0x1400003) where dpy is a Display object to get a Window object on that display for an existing window with the given XID.

Example usage:

>>> import Xlib
>>> import Xlib.display
>>> dpy = Xlib.display.Display()
>>> win = dpy.create_resource_object('window', 0x277075e)
>>> win.get_wm_class()
('gnome-terminal', 'Gnome-terminal')
Underclassman answered 25/2, 2014 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.