Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过Python计算Linux上具有特定标题的窗口数?_Python_Linux_X11 - Fatal编程技术网

如何通过Python计算Linux上具有特定标题的窗口数?

如何通过Python计算Linux上具有特定标题的窗口数?,python,linux,x11,Python,Linux,X11,这是我的解决办法,但我认为这不是一个好办法。有人能给我推荐一个新的解决方案吗?我想枚举windows,并通过使用python的核心库(而不是wnck或pygtk)获得要比较的Title属性 def linux_CountWindowsByTitle(title): import commands XWinInfoOutput = commands.getoutput("xwininfo -all -root") resArray = XWinInfoOutput.spli

这是我的解决办法,但我认为这不是一个好办法。有人能给我推荐一个新的解决方案吗?我想枚举windows,并通过使用python的核心库(而不是wnck或pygtk)获得要比较的Title属性

def linux_CountWindowsByTitle(title):
    import commands
    XWinInfoOutput = commands.getoutput("xwininfo -all -root")
    resArray = XWinInfoOutput.split("\n")
    resRange = len(resArray) - 1
    res = 0
    #Parse each line of output
    for i in range(0, resRange):
        index = resArray[i].find("\"")   #Get index of Double quote
        if (index < 0):
            continue    #This line does not have title we need

        tmp = resArray[i].split("\": (")[0] #Remove tail
        windowName = tmp.split("\"",1)[1]   #Remove head
        if (UTILITY.Compare(title, windowName)):
            #LIBRARY.Report(windowName)
            res += 1

    return res
def linux\u CountWindowsByTitle(标题):
导入命令
XWinInfoOutput=commands.getoutput(“xwininfo-all-root”)
resArray=XWinInfoOutput.split(“\n”)
resRange=len(重新排列)-1
res=0
#解析输出的每一行
对于范围内的i(0,resRange):
index=resArray[i]。查找(“\”)#获取双引号的索引
如果(指数<0):
继续#此行没有我们需要的标题
tmp=resArray[i]。拆分(“\”:(”[0]#删除尾部
windowName=tmp.split(“\”,1)[1]#移除磁头
如果(实用程序比较(标题、窗口名)):
#LIBRARY.Report(windowName)
res+=1
返回res

您可以使用模块
wnck

import wnck

screen = wnck.screen_get_default()
window_list = wnck.Screen.get_windows(screen)

window_names = [ w.get_name() for w in window_list ]
为了计算类似窗口的数量,您可以创建字典:

count = window_names.count
wcounts = { item: item.count(item) for item in set(window_names) }
其中,字典将窗口标题作为键,该值将是相同名称重复的次数

稍有不同,但您可能会发现有趣的用法是:

wdict = { w.get_name(): w for w in window_list }
wdict.has_key(title)
如果以后需要对窗口进行其他处理,您仍然可以在
wdict
中找到一个参考。例如,您可以检查属性、将其最大化、最小化以及窗口管理器将执行的所有典型操作


注意:对于较新版本的
wnck
(>=3.0),您必须使用PyGObject(GObject内省),但你明白了。

非常感谢你的回答,我还没有尝试你的解决方案,但它似乎是我需要的好解决方案。在这种情况下,你需要为
Xlib
提供包装,但它是低级IMHO。嗯,我为公司编写代码,所以我不能随意使用库。我必须尝试解决核心库的问题:)为了将来的参考,你应该在你的问题中提到它。很抱歉,我在stackoverflow上没有经验,我会记住你的话