如何使用python-xlib在所有工作区中搜索窗口?

时间:2012-11-22 23:16:34

标签: python workspace xlib

我会创建一个Python脚本,搜索具有特定标题的窗口并将其置于顶部。 我发现如何在当前工作区中执行此操作: http://ubuntuforums.org/showthread.php?t=1254376

但我想在所有工作区中完成。我该怎么办?

2 个答案:

答案 0 :(得分:0)

老问题,但我遇到了同样的问题。 答案是,要获取所有工作区的窗口列表,您不应该测试窗口是否可见(删除“attrs.map_state == X.IsViewable”部分)。

display = Display()
root = display.screen().root
winid_list = root.get_full_property(self.NET_CLIENT_LIST_ATOM,
                                            X.AnyPropertyType).value
for winid in winid_list:
    win = self.display.create_resource_object('window', winid)
    transient_for = win.get_wm_transient_for()
    wmname = win.get_wm_name()
    if transient_for == None:
        if wmname != None and name in wmname:
            break

答案 1 :(得分:0)

代码在查询树中搜索窗口,并获取窗口对象,该对象以后可用于获取所需的属性:

disp = Display() 
root = disp.screen().root    
children = root.query_tree().children    
for win in children:
    try:
        WinName = getProp(disp,win,'NAME') # or win.get_wm_name()
        PIDs = getProp(disp,win,'PID')
        WinPID = newPIDs[0]

        if WinName and WinName == DESIRED_NAME:
            print("found the window with pid: {} winName: {}  winId: {}".format(pid, newWinName,win.id))
            # Code to bringToTop
        print

    except Xlib.error.BadWindow:
        print("BadWindow error")


def getProp(disp,win, prop):
    p = win.get_full_property(disp.intern_atom('_NET_WM_' + prop), 0)
    return [None] if (p is None) else p.value