使用Python和Xorg获取当前窗口标题 [英] Get current window title with Python and Xorg
问题描述
在stackoverflow回答了我关于Wiimote左/右键单击问题的上一个问题之后,不仅可以移动鼠标光标,而且现在还可以左/右单击内容.我现在还有一个问题.
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.
我在python中使用什么来获取当前活动窗口的标题?在对"X11 Python窗口标题","Linux Python窗口标题"和类似内容进行谷歌搜索之后,我发现的只是win32和tkinker(还是?),这不是我所需要的.
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!
推荐答案
编辑
最佳方式:
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
这篇关于使用Python和Xorg获取当前窗口标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!