Python:如何使用 Xlib 在工作区之间切换?
如何使用带有 Xlib 模块的 Python 在窗口管理器的工作区之间切换?
这是我最有希望的尝试:
#!/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])
上面的代码是从 PyPanel 源代码的各个地方无耻地窃取的;不幸的是,它没有做任何事情,甚至没有生成警告/异常。我在这里错过了什么吗?
我正在使用 Python 和 PyGTK。 Xlib 似乎是切换桌面的正确选择。我不打算使用 wnck (有缺陷的 Python 模块)或类似的,但无论如何我都会很感激任何指针。
我想补充一点,这是我第一次尝试使用 Xlib(或 PyGTK)编写 Python 应用程序。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然,您需要处理同一个 Display 对象,然后在最后
刷新
它。类似于:信用:来自非常相似的线程的想法(这几乎有效)。
PS顺便说一句,桌面号是从0开始的。
Apparently you need to work on the same Display object and then
flush
it at the end. Something like:Credit: Idea from a very similar thread (which almost works).
P.S. By the way, the desktop number starts from 0.