如何通过id获取Xlib.display.Window实例?

时间:2014-02-25 12:53:15

标签: python xlib

我找到了以下代码(http://pastebin.com/rNkUj5V8),但我宁愿使用直接查找:

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()

我试过直接实例化一个Window对象,但是对get_attributes的调用失败了:

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

1 个答案:

答案 0 :(得分:5)

使用dpy.create_resource_object('window', 0x1400003)其中dpyDisplay对象,在该显示中为具有给定XID的现有窗口获取Window对象。

使用示例:

>>> import Xlib
>>> import Xlib.display
>>> dpy = Xlib.display.Display()
>>> win = dpy.create_resource_object('window', 0x277075e)
>>> win.get_wm_class()
('gnome-terminal', 'Gnome-terminal')