用python获取窗口标题?

发布于 2024-08-07 09:29:08 字数 287 浏览 3 评论 0原文

我正在尝试编写一个Python程序,每隔X秒检查一次“窗口标题”是否为“last.fm”(http://www.last.fm/download)更改了,如果确实如此(或者这是我第一次运行该程序),它应该使用从窗口标题捕获的字符串来搜索歌曲的歌词并将它们显示给用户。

我目前使用 KDE4 作为桌面环境,我只需要“指向正确的方向”即可捕获属于 last.fm 客户端窗口标题的字符串。

谢谢!

I'm trying to write a python program that checks every X seconds if the 'window title' for 'last.fm' (http://www.last.fm/download) changed, if it did (or it's the first time I run the program) it should use use the string captured from the window title to search for the song's lyrics and display them to the user.

I'm currently using KDE4 as my desktop environment and I just need to be 'pointed in the right direction' on how I can capture the string that belongs to the window title for last.fm client.

Thanks!

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

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

发布评论

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

评论(5

此刻的回忆 2024-08-14 09:29:08

您可以通过 subprocess 模块使用 wmctrl 实用程序。您可以在终端中输入 wmctrl -l 并查看从中获得的输出。

You can use the wmctrl utility through the subprocess module. You can type wmctrl -l into a terminal and see the output you can get from it.

情徒 2024-08-14 09:29:08

我认为通过使用自动化框架,您也许能够将其作为一个子集来实​​现。
例如尝试dogtail(https://fedorahosted.org/dogtail/),它可以通过名称聚焦于窗口,按名称单击按钮,因此在 src 代码中您也许能够看到如何获取标题。

I think by using a automation framework you may be able to achieve this as a subset.
e.g. try dogtail(https://fedorahosted.org/dogtail/), it can focus on windows by name, click on buttons by name, so in the src code you may be able to see how to get title.

一梦浮鱼 2024-08-14 09:29:08

查看 X11 实用程序,特别是 xlsclientsxprop

举个例子,这是我用来获取有关我的 Firefox 信息的 shell 命令
窗口:

id_=$(xlsclients -al|grep "Command:  firefox-bin" -A1 -B4|head -n1|cut -d ' ' -f 2|tr -d ':')
xprop -id "$_id"

输出:

SM_CLIENT_ID(STRING) = "1181f048b9000125508490000000037360008"
WM_CLASS(STRING) = "firefox-bin", "Firefox-bin"
WM_COMMAND(STRING) = { "firefox-bin" }
WM_CLIENT_LEADER(WINDOW): window id # 0x0
_NET_WM_PID(CARDINAL) = 4265
WM_LOCALE_NAME(STRING) = "no_NO"
WM_CLIENT_MACHINE(STRING) = "gnom.ifi.uio.no"
WM_NORMAL_HINTS(WM_SIZE_HINTS):
                program specified size: 10 by 10
WM_PROTOCOLS(ATOM): protocols  WM_DELETE_WINDOW, WM_TAKE_FOCUS, _NET_WM_PING
WM_ICON_NAME(STRING) = "firefox-bin"
_NET_WM_ICON_NAME(UTF8_STRING) = 0x66, 0x69, 0x72, 0x65, 0x66, 0x6f, 0x78, 0x2d, 0x62, 0x69, 0x6e
WM_NAME(STRING) = "Firefox"
_NET_WM_NAME(UTF8_STRING) = 0x46, 0x69, 0x72, 0x65, 0x66, 0x6f, 0x78

Have a look at X11 utilities, specifically xlsclients and xprop.

As an example, this is the shell commands I used to get info about my firefox
window:

id_=$(xlsclients -al|grep "Command:  firefox-bin" -A1 -B4|head -n1|cut -d ' ' -f 2|tr -d ':')
xprop -id "$_id"

Output:

SM_CLIENT_ID(STRING) = "1181f048b9000125508490000000037360008"
WM_CLASS(STRING) = "firefox-bin", "Firefox-bin"
WM_COMMAND(STRING) = { "firefox-bin" }
WM_CLIENT_LEADER(WINDOW): window id # 0x0
_NET_WM_PID(CARDINAL) = 4265
WM_LOCALE_NAME(STRING) = "no_NO"
WM_CLIENT_MACHINE(STRING) = "gnom.ifi.uio.no"
WM_NORMAL_HINTS(WM_SIZE_HINTS):
                program specified size: 10 by 10
WM_PROTOCOLS(ATOM): protocols  WM_DELETE_WINDOW, WM_TAKE_FOCUS, _NET_WM_PING
WM_ICON_NAME(STRING) = "firefox-bin"
_NET_WM_ICON_NAME(UTF8_STRING) = 0x66, 0x69, 0x72, 0x65, 0x66, 0x6f, 0x78, 0x2d, 0x62, 0x69, 0x6e
WM_NAME(STRING) = "Firefox"
_NET_WM_NAME(UTF8_STRING) = 0x46, 0x69, 0x72, 0x65, 0x66, 0x6f, 0x78
南烟 2024-08-14 09:29:08

尝试使用 dcop 和引导 kwin。您也许可以列出所有窗口标题。

有关如何使用 dcop 的示例,请参阅以下内容:
http://docs.kde.org/stable/en/kdegraphics/ ksnapshot/dcop.html

Try using dcop and piloting kwin. You can probably list all the window titles.

See the following for an example on how to use dcop:
http://docs.kde.org/stable/en/kdegraphics/ksnapshot/dcop.html

风透绣罗衣 2024-08-14 09:29:08

另一个答案可能是检查应用程序是否将歌曲更改发布到 DBus。如果确实如此,那么您只需监听该事件并采取行动即可。

another answer maybe to check if the application publishes the song change to DBus. if it does then you can just listen for the event and act on that.

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