1
如何在使用Xlib的python應用程序中爲根窗口(或任何其他窗口)設置光標?Python xlib更改光標
我有一個display
和window
(根窗口)的實例。 使用C綁定;我可以用我用XCreatePixmapCursor創建的遊標使用XDefineCursor。我如何做與python綁定相同的?
我希望能夠使用默認光標或自定義光標。
如何在使用Xlib的python應用程序中爲根窗口(或任何其他窗口)設置光標?Python xlib更改光標
我有一個display
和window
(根窗口)的實例。 使用C綁定;我可以用我用XCreatePixmapCursor創建的遊標使用XDefineCursor。我如何做與python綁定相同的?
我希望能夠使用默認光標或自定義光標。
有你要記住,當你需要找到的python-Xlib的任何等價的libx11功能的兩件事情:
XCreatePixmapCursor()
轉換爲pixmap.create_cursor()
。XDefineCursor()
,你會發現它實際上是調用XChangeWindowAttributes()
,這意味着你想在python-xlib中使用win.change_attributes()
。如果你想使用XCreateFontCursor()
使用光標光標字體,第二條準又適用:它調用的引擎蓋,這相當於font.create_glyph_cursor()
下XCreateGlyphCursor()
。
把所有的一起,這裏就是你會得到什麼:
# Create font cursor
font = display.open_font('cursor')
cursor = font.create_glyph_cursor(font, Xlib.Xcursorfont.crosshair, Xlib.Xcursorfont.crosshair+1, (65535, 65535, 65535), (0, 0, 0))
# Use PIL to load a cursor image and ensure that it's 1-bit as required
im = Image.open('cursor.png').convert('1')
w, h = im.size
# Create pixmap cursor
mask = win.create_pixmap(w, h, 1)
gc = mask.create_gc(foreground=0, background=1)
mask.put_pil_image(gc, 0, 0, im)
cursor = mask.create_cursor(mask, (0, 0, 0), (65535, 65535, 65535), 0, 0)
# Change cursors for given windows
win.change_attributes(cursor=cursor)
如果你想知道關於+1
的呼叫的意義font.create_glyph_cursor()
,這是在source code of XCreateFontCursor()
解釋。