from collections import namedtuple
from Xlib.display import Display
from Xlib import Xatom
display = Display()
root = display.screen(0)['root']
StrutPartial = namedtuple('StrutPartial', 'left right top bottom'
' left_start_y left_end_y right_start_y right_end_y'
' top_start_x, top_end_x, bottom_start_x bottom_end_x')
def query_property(win, name, prop_type, format_size, empty=None):
if isinstance(win, int):
# Create a Window object for the Window ID
win = display.create_resource_object('window', win)
if isinstance(name, str):
# Create/retrieve the X11 Atom for the property name
name = display.get_atom(name)
result = win.get_full_property(name, prop_type)
if result and result.format == format_size:
return result.value
return empty
window_ids = ([root.id] +
list(query_property(root, '_NET_CLIENT_LIST', Xatom.WINDOW, 32, [])))
for wid in window_ids:
result = query_property(wid, '_NET_WM_STRUT_PARTIAL', Xatom.CARDINAL, 32)
if result:
# Toss it in a namedtuple to avoid needing opaque numeric indexes
strut = StrutPartial(*result)
print(strut)
不要使用
get_property
,而是使用get_full_property
。它隐藏了XGetWindowProperty
的API的奇怪方面。在我不得不浏览pythonxlib源代码以找到它的使用示例,但下面是我最后编写的一个简化版本,以使用它。在
来自this spec的
_NET_CLIENT_LIST
的定义:…以及来自this spec的
^{pr2}$_NET_WM_STRUT_PARTIAL
的定义:…转换为以下Python代码:
…打印此示例输出:
(}相同,但是
result.value
是12个32位整数的array.array
,因为规范要求将_NET_WM_STRUT_PARTIAL
设置为CARDINAL[12]/32
。您可以迭代或索引array.array
,方法与普通的list
或{namedtuple
是使用命名属性的简单、舒适的方法。)它就在文档中:
http://python-xlib.sourceforge.net/doc/html/python-xlib_21.html#SEC20
相关问题 更多 >
编程相关推荐