python - cycling through X11 windows python-xlib
问题描述
I'm trying to iterate through a list of python-xlib window objects so to set their relative stacking order on the display. My goal here is to present a sort of carousel viewing keyboard shortcut for windows stacked on top of each other, by grabbing a select list of windows, and then stacking them relative to some order. So far however I've only been able to push forward/backward the current active window.
Q:
How can I iterate over these windows so to set their relative orders, and then paint those windows to the display? A backup solution might be to push the active focus to one of these windows, but I haven't been able to get that to work either.
Below are the relevant pieces of code. The full project source code can be found at https://github.com/1mikegrn/snappyzones/tree/cycle_windows/src/snappyzones
def active_window(display, window_id=None):
if not window_id:
window_id = (
display.screen()
.root.get_full_property(
display.intern_atom("_NET_ACTIVE_WINDOW"), X.AnyPropertyType
)
.value[0]
)
try:
return display.create_resource_object("window", window_id)
except XError:
return None
def cycle_windows(self, keysym):
try:
display = Display()
window = active_window(display).query_tree().parent.query_tree().parent
root = display.screen().root
tree = root.query_tree()
stack = [
item
for item in tree.children
if all(
getattr(item.get_geometry(), x) == getattr(window.get_geometry(), x)
for x in ("x", "y", "width", "height")
)
]
if keysym == XK.XK_Up:
item = active_window(display, stack[0].id)
item.configure(stack_mode=X.Below)
display.set_input_focus(stack[1], X.RevertToParent, X.CurrentTime)
elif keysym == XK.XK_Down:
item = active_window(display, stack[-1].id)
item.configure(stack_mode=X.Above)
display.sync()
except BadWindow as e:
print(e)
解决方案
推荐阅读
- android - A: 重新安装应用程序后尝试访问 MediaStore 时出现 FileNotFoundException
- mysql - 使用 JSON_ARRAY_APPEND 将大数组插入 JSON 文档
- r - 重写反向代理的复杂路径
- c++ - C++程序没有使用向量进入for循环
- powerbuilder - 在数据窗口中列出行,并在另一个 powerbuilder 中编辑行
- pandas - 错误“utf-8”编解码器无法解码字节 0xf4
- sql - 带有 where 子句的右外连接不返回预期结果
- javascript - Jquery 切换持续时间似乎不起作用
- java - 为什么我的方法在我验证它在junit中被调用后继续工作?
- webpack - ModuleFederationPlugin 的“共享”配置中的“单例”选项究竟是如何工作的?