gpt4 book ai didi

python - 如何使用 Python 的 Xlib 将文本写入根窗口?

转载 作者:行者123 更新时间:2023-12-01 04:24:26 24 4
gpt4 key购买 nike

我正在运行带有 dwm 的 Debian 10 稳定 x64 系统窗口管理器,我使用的是 Python 3.7.3。据我所知some example code draw_text method itself ,我应该能够使用这样的代码在根窗口上绘制文本:

#!/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!")

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

最佳答案

您的代码应该在根窗口上绘制文本是对的。你只需要:

  • 确保您的背景确实是根窗口(xwininfo 很棒)
  • 再次检查坐标:(i) 如果 dwm ,默认情况下,显示顶栏,它可能隐藏文本。或者只是 [Alt]+b,切换栏 (ii) 如果您有其他窗口,例如您的终端,在顶部,您将看不到文本。
  • 最后执行 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

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

    如果要防止文本消失,则需要一个循环:
  • 要么是 while True按原样循环代码,无论如何都会重绘它
  • 或者,更好的是一个事件循环,它只会在必要时重绘它(见下文)

  • 公开事件(引用 https://tronche.com/gui/x/xlib/events/exposure/expose.htmlhttp://python-xlib.sourceforge.net/doc/html/python-xlib_13.html#SEC12)

    are generated when regions of a window has to be redrawn



    但是,如果我们监听 root 窗口的 Exposure 事件,我们不会得到任何信息(原因:(参见 dwm 源代码中的 setup 函数)root 没有 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()

    关于python - 如何使用 Python 的 Xlib 将文本写入根窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59472652/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com