代码之家  ›  专栏  ›  技术社区  ›  dutt

在X中获取活动窗口标题

  •  16
  • dutt  · 技术社区  · 15 年前

    我想知道活动窗口的标题。该应用程序是一个后台任务,因此如果用户让Eclipse打开,函数将返回“Eclipse-blabla”,因此它不会获得我自己窗口的窗口标题。我使用PyQt4在Python 2.6中开发这个。

    def get_active_window_title():
        title = ''
        root_check = ''
    
        root = Popen(['xprop', '-root'],  stdout=PIPE)
    
        if root.stdout != root_check:
            root_check = root.stdout
    
            for i in root.stdout:
                if '_NET_ACTIVE_WINDOW(WINDOW):' in i:
                    id_ = i.split()[4]
                    id_w = Popen(['xprop', '-id', id_], stdout=PIPE)
    
            for j in id_w.stdout:
                if 'WM_ICON_NAME(STRING)' in j:
                    if title != j.split()[2]:
                        return j.split("= ")[1].strip(' \n\"')
    

    它适用于大多数窗口,但不是所有窗口。例如,它找不到我的kopete聊天窗口,也找不到我正在开发的应用程序的名称。

    我的下一次尝试是这样的:

    def get_active_window_title(self):
        screen = wnck.screen_get_default()
        if screen == None:
            return "Could not get screen"
        window = screen.get_active_window()
        if window == None:
            return "Could not get window"
        title = window.get_name()
        return title;
    

    但出于某种原因 总是没有。

    是否有人有更好的方法获得当前窗口标题,或如何修改我的一种方法,适用于所有窗口?

    编辑:

    def get_active_window_title(self):
        root_check = ''
        root = Popen(['xprop', '-root'],  stdout=PIPE)
    
        if root.stdout != root_check:
            root_check = root.stdout
    
            for i in root.stdout:
                if '_NET_ACTIVE_WINDOW(WINDOW):' in i:
                    id_ = i.split()[4]
                    id_w = Popen(['xprop', '-id', id_], stdout=PIPE)
            id_w.wait()
            buff = []
            for j in id_w.stdout:
                buff.append(j)
    
            for line in buff:
                match = re.match("WM_NAME\((?P<type>.+)\) = (?P<name>.+)", line)
                if match != None:
                    type = match.group("type")
                    if type == "STRING" or type == "COMPOUND_TEXT":
                        return match.group("name")
            return "Active window not found"
    
    3 回复  |  直到 15 年前
        1
  •  8
  •   Falmarri    15 年前

    xdotool 能做到的。

    xdotool getactivewindow

        2
  •  9
  •   Alex Spurling danprice    15 年前

    我稍微修改了您的解决方案,以便它能够更有效地运行(它将参数传递给xprop,因此只返回它需要的数据)。另外,我不确定是否有必要缓冲xprop的输出,所以我去掉了它。如果由于某种原因找不到活动窗口,它还应该更正返回“Active window not found”。

    def get_active_window_title(self):
        root = Popen(['xprop', '-root', '_NET_ACTIVE_WINDOW'], stdout=PIPE)
    
        for line in root.stdout:
            m = re.search('^_NET_ACTIVE_WINDOW.* ([\w]+)$', line)
            if m != None:
                id_ = m.group(1)
                id_w = Popen(['xprop', '-id', id_, 'WM_NAME'], stdout=PIPE)
                break
    
        if id_w != None:
            for line in id_w.stdout:
                match = re.match("WM_NAME\(\w+\) = (?P<name>.+)$", line)
                if match != None:
                    return match.group("name")
    
        return "Active window not found"
    
        3
  •  1
  •   Eyal Levin    8 年前

    xdotool :

    $ xdotool getactivewindow getwindowname
    
        4
  •  0
  •   Dan D.    6 年前

    这太晚了,不可能有用,但它确实有效,而且我有一些程序可以使用 wnck .

    这个 wnck公司 screen.force_update() 之前 会有用的。没有这个 wnck公司

    def get_active_window_title(self):
        screen = wnck.screen_get_default()
        if screen is None:
            return "Could not get screen"
        screen.force_update()
        window = screen.get_active_window()
        if window is None:
            return "Could not get window"
        title = window.get_name()
        return title
    
        5
  •  0
  •   brezniczky    6 年前

    我看到现在这个问题有点模糊了,而且对Python 2的支持已经接近其预定生命周期的末尾,所以我想我应该提到一个更为Python 3'ic的版本。

    因此,下面的内容可能更充分,对新蟒蛇的资源需求也更少:

    (同样,没有 with

        with Popen(['xprop', '-root', '_NET_ACTIVE_WINDOW'], stdout=PIPE) as root:
            for line in root.stdout:
                line = str(line, encoding="UTF-8")
    
                m = re.search('^_NET_ACTIVE_WINDOW.* ([\w]+)$', line)
                if m is not None:
                    id_ = m.group(1)
                    with Popen(['xprop', '-id', id_, 'WM_NAME'],
                               stdout=PIPE) as id_w:
                        for line in id_w.stdout:
                            line = str(line, encoding="UTF-8")
                            match = re.match("WM_NAME\(\w+\) = \"(?P<name>.+)\"$",
                                             line)
                        if match is not None:
                            return match.group("name")
                    break
        return "Active window not found"