使用 Python 和 Xorg 获取当前窗口标题

发布于 2024-09-16 07:38:43 字数 250 浏览 15 评论 0原文

在 stackoverflow 回答了我之前关于 Wiimote 左/右键单击问题的问题后,我不仅可以移动鼠标光标,现在还可以左/右键单击事物。我现在还有一个问题。

我在 python 中使用什么来获取当前活动窗口的标题?在谷歌搜索“X11 Python Window Title”、“Linux Python Window Title”和类似内容之后,我发现的只是 win32 和 tkinker (又?),这不是我需要的。

如果你能帮忙,那就太好了!

After stackoverflow answered my previous question on here about my Wiimote left/right click issue, Not only can I move the mouse cursor, I can now left/right click on things. I now have one more question.

What do I use in python to get the title of the current active window? After googling 'X11 Python Window Title', 'Linux Python Window Title' and things similar, All I've found is win32 and tkinker (again?), which isn't what I need.

If you could help, That would be awesome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

感性 2024-09-23 07:38:43

编辑

最佳方法:

import gtk
import wnck
import glib

class WindowTitle(object):
    def __init__(self):
        self.title = None
        glib.timeout_add(100, self.get_title)

    def get_title(self):
        try:
            title = wnck.screen_get_default().get_active_window().get_name()
            if self.title != title:
                self.title  = title
                print title
        except AttributeError:
            pass
        return True

WindowTitle()
gtk.main()

替代方法:

from subprocess import PIPE, Popen
import time

title = ''
root_check = ''

while True:
    time.sleep(0.6)
    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]:
                    title = j.split()[2]
                    print "current window title: %s" % title

EDIT

best way:

import gtk
import wnck
import glib

class WindowTitle(object):
    def __init__(self):
        self.title = None
        glib.timeout_add(100, self.get_title)

    def get_title(self):
        try:
            title = wnck.screen_get_default().get_active_window().get_name()
            if self.title != title:
                self.title  = title
                print title
        except AttributeError:
            pass
        return True

WindowTitle()
gtk.main()

Alternative way:

from subprocess import PIPE, Popen
import time

title = ''
root_check = ''

while True:
    time.sleep(0.6)
    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]:
                    title = j.split()[2]
                    print "current window title: %s" % title
行雁书 2024-09-23 07:38:43

我注意到 wnck 需要 GTK 事件循环来更新活动窗口。 Xlib则不存在这样的问题:

import Xlib
import Xlib.display
disp = Xlib.display.Display()
window = disp.get_input_focus().focus

# Get active window class and name
window.get_wm_class()
window.get_wm_name()

I noticed that wnck requires GTK event loop to update the active window. There is no such problem with Xlib:

import Xlib
import Xlib.display
disp = Xlib.display.Display()
window = disp.get_input_focus().focus

# Get active window class and name
window.get_wm_class()
window.get_wm_name()
川水往事 2024-09-23 07:38:43

Killown 的基于 xprop 的解决方案可以压缩为单个(虽然很长)语句:

import subprocess
def GetActiveWindowTitle():
    return subprocess.Popen(["xprop", "-id", subprocess.Popen(["xprop", "-root", "_NET_ACTIVE_WINDOW"], stdout=subprocess.PIPE).communicate()[0].strip().split()[-1], "WM_NAME"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].strip().split('"', 1)[-1][:-1]

killown's xprop-based solution can be compacted into a single (though lengthy) statement:

import subprocess
def GetActiveWindowTitle():
    return subprocess.Popen(["xprop", "-id", subprocess.Popen(["xprop", "-root", "_NET_ACTIVE_WINDOW"], stdout=subprocess.PIPE).communicate()[0].strip().split()[-1], "WM_NAME"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].strip().split('"', 1)[-1][:-1]
巡山小妖精 2024-09-23 07:38:43

使用 ewmh

from ewmh import EWMH
wm = EWMH()
win = wm.getActiveWindow()
win_name = win.get_wm_name()
print(win_name)

With ewmh:

from ewmh import EWMH
wm = EWMH()
win = wm.getActiveWindow()
win_name = win.get_wm_name()
print(win_name)
晒暮凉 2024-09-23 07:38:43

我认为如果你想处理 Windows 和 Windows,python-wnck 可能会很有用。工作区和这样的。我无法立即找到 Python 文档,但根据它包装的 libwnck C 库的文档,它有一个 wnck_screen_get_active_window() 方法。

I think python-wnck might be useful if you want to handle Windows & workspaces & such. I can't find the Python docs immediately, but according to the docs for the libwnck C library that it wraps, it has a wnck_screen_get_active_window() method.

情痴 2024-09-23 07:38:43

我的解决方案:

import wnck
disp=Display()
default_screen=wnck.screen_get_default()
default_screen.force_update()

active_window=disp.create_resource_object('window', default_screen.get_active_window().get_xid())
title=active_window.get_wm_name()

My solution:

import wnck
disp=Display()
default_screen=wnck.screen_get_default()
default_screen.force_update()

active_window=disp.create_resource_object('window', default_screen.get_active_window().get_xid())
title=active_window.get_wm_name()
酷遇一生 2024-09-23 07:38:43

问题是找出哪个窗口是活动的或者标题是什么?
获取窗口的标题很容易:

MainWindow.title()

,其中 MainWindow 是窗口的名称。但不知道活动窗口。从来没有多个窗口。

Is the problem to find out which window is active or what the title is?
Getting a window's title is easy:

MainWindow.title()

,where MainWindow is the window's name. No idea about active window though. Never had multiple windows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文