Python: how to switch between workspaces using Xlib?

How do I switch between window manager workspaces using Python with the Xlib module?

This is my most promising attempt:

#!/usr/bin/python from Xlib import X, display, error, Xatom, Xutil import Xlib.protocol.event screen = Xlib.display.Display().screen() root = screen.root def sendEvent(win, ctype, data, mask=None): """ Send a ClientMessage event to the root """ data = (data+[0]*(5-len(data)))[:5] ev = Xlib.protocol.event.ClientMessage(window=win, client_type=ctype, data=(32,(data))) if not mask: mask = (X.SubstructureRedirectMask|X.SubstructureNotifyMask) root.send_event(ev, event_mask=mask) # switch to desktop 2 sendEvent(root, Xlib.display.Display().intern_atom("_NET_CURRENT_DESKTOP"), [2]) 

The above code is shamelessly stolen from different places in the PyPanel source; Unfortunately, it does nothing, does not even generate a warning / exception. Did I miss something?

I am using Python and PyGTK. Xlib seems to be the right choice for switching desktops. I do not intend to use wnck (the Python buggy module) or similar, but I would still recognize any pointers.

I can add that this is my first attempt to write a Python application using Xlib (or PyGTK).

+4
source share
1 answer

Obviously you need to work with the same Display object and then flush at the end. Sort of:

 display = Xlib.display.Display() screen = display.screen() root = screen.root # ... sendEvent(root, display.intern_atom("_NET_CURRENT_DESKTOP"), [1, X.CurrentTime]) display.flush() 

Credit: The idea is a very similar stream (which almost works).

PS By the way, the desktop number starts at 0.

+2
source

Source: https://habr.com/ru/post/1303517/


All Articles