简体   繁体   English

用python获取窗口标题?

[英]Get window title with python?

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. 我正在尝试编写一个python程序,如果'last.fm'( http://www.last.fm/download )的'窗口标题'发生了变化,那么每隔X秒检查一次,如果是的话(或者是第一次)我运行程序)应该使用从窗口标题中捕获的字符串来搜索歌曲的歌词并将其显示给用户。

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. 我目前正在使用KDE4作为我的桌面环境,我只需要“指向正确的方向”我如何捕获属于last.fm客户端窗口标题的字符串。

Thanks! 谢谢!

You can use the wmctrl utility through the subprocess module. 您可以通过subprocess wmctrl模块使用wmctrl实用程序。 You can type wmctrl -l into a terminal and see the output you can get from it. 您可以在终端中键入wmctrl -l并查看可以从中获取的输出。

I think by using a automation framework you may be able to achieve this as a subset. 我认为通过使用自动化框架,您可以将其作为子集实现。 eg 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. 例如,尝试dogtail( https://fedorahosted.org/dogtail/ ),它可以按名称关注窗口,按名称点击按钮,所以在src代码中你可以看到如何获得标题。

Have a look at X11 utilities, specifically xlsclients and xprop . 看看X11实用程序,特别是xlsclientsxprop

As an example, this is the shell commands I used to get info about my firefox window: 作为一个例子,这是我用来获取有关我的firefox窗口的信息的shell命令:

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

Try using dcop and piloting kwin. 尝试使用dcop和驾驶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 有关如何使用dcop的示例,请参阅以下内容: http ://docs.kde.org/stable/en/kdegraphics/ksnapshot/dcop.html

another answer maybe to check if the application publishes the song change to DBus. 另一个答案可能是检查应用程序是否将歌曲更改发布到DBus。 if it does then you can just listen for the event and act on that. 如果它确实那么你可以只听取事件并采取行动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM