首页 > 解决方案 > 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)

标签: pythondesktop-applicationxlibxorgxserver

解决方案


推荐阅读