Python: how to switch between workspaces using Xlib?

pygtk, python, window, xlib

Solution

Apparently you need to work on the same Display object and then `flush` it at the end. Something like:

display = Xlib.display.Display()
screen = display.screen()
root = screen.root

# ...

sendEvent(root, display.intern_atom("_NET_CURRENT_DESKTOP"), [1, X.CurrentTime])
display.flush()

Credit: Idea from a very similar thread (which almost works).

P.S. By the way, the desktop number starts from 0.

Problem

How do I switch between my window manager's workspaces using Python with 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 various places in the PyPanel source; unfortunately, it doesn't do anything, not even generate a warning / exception. Am I missing something here? I'm using Python and PyGTK. Xlib seems to be the right choice for switching desktops. I don't intend to use wnck (buggy Python module) or similar, but I'd appreciate any pointers anyway. I might add that this is my first attempt at writing a Python application using Xlib (or PyGTK).

Original source