如何使用 Python 的 Xlib 将文本写入根目录 window?

How do I write text to the root window using Python's Xlib?

我是 运行 Debian 10 stable x64 系统 dwm window manager, and I'm using Python 3.7.3. From what I can tell from some example code and the draw_text method itself,我应该可以使用如下代码在根 window 上绘制文本:

#!/usr/bin/env python3

import Xlib
import Xlib.display

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

gc = root.create_gc(foreground = screen.white_pixel, background = screen.black_pixel)
root.draw_text(gc, 1, 1, b"Hello, world!")

这段代码运行没有错误,但没有显示任何文本。我也尝试过不同的坐标,但没有任何运气。我的根 window 背景只是默认的黑色,所以我不认为文本无法显示,因为我将前景像素颜色设置为白色。

你的代码应该在根 window 上绘制文本,你说得对。您只需要:

  • 确保你的后台确实是根window(xwininfo很好)
  • 再次检查坐标:(i) 如果 dwm 默认情况下显示顶部栏,它可能会隐藏文本。或者只是 [Alt]+b,切换栏 (ii) 如果您有其他 windows,例如您的终端,在顶部,您将看不到文本。
  • 最后执行一次XFlush。没有它,请求将保留在客户端中。

此处有效的代码(Gentoo amd64/desktop/stable, dwm-6.2, python-3.6.9):

 #!/usr/bin/env python3

import Xlib
import Xlib.display

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

gc = root.create_gc(foreground = screen.white_pixel, background = screen.black_pixel)
root.draw_text(gc, 100, 100, b"Hello, world!")  # changed the coords more towards the center

display.flush()  # To actually send the request to the server

请注意,如果其他 windows 重叠或刷新该位置,文本将会消失。文本会一直保留,例如,您将 window 移动到(擦除它),或者您更改为另一个具有 window 覆盖这些坐标的 dwm-Tab。

如果要防止文字消失,需要一个循环:

  • 按原样对代码进行 while True 循环,无论如何都会重新绘制它
  • 或者,更好的是,一个事件循环,它只会在必要时重绘它(见下文)

曝光事件(参考https://tronche.com/gui/x/xlib/events/exposure/expose.html and http://python-xlib.sourceforge.net/doc/html/python-xlib_13.html#SEC12

are generated when regions of a window has to be redrawn

但是,如果我们侦听根 window 的公开事件,我们会得到 none(原因:(请参阅 dwm 源代码中的 setup 函数)没有 ExposureMask根)。我尝试和工作的内容:

#!/usr/bin/env python3

import Xlib
from Xlib import display, X   # X is also needed

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

#print(root.get_attributes())
root.change_attributes(event_mask=X.ExposureMask)  # "adds" this event mask
#print(root.get_attributes())  # see the difference

gc = root.create_gc(foreground = screen.white_pixel, background = screen.black_pixel)

def draw_it():
    root.draw_text(gc, 100, 100, b"Hello, world!")
    display.flush()

draw_it()
while 1:
    if display.pending_events() != 0:  # check to safely apply next_event
        event = display.next_event()
        if event.type == X.Expose and event.count == 0:
            draw_it()