2017-01-09 95 views
1

如何在使用Xlib的python應用程序中爲根窗口(或任何其他窗口)設置光標?Python xlib更改光標

我有一個displaywindow(根窗口)的實例。 使用C綁定;我可以用我用XCreatePixmapCursor創建的遊標使用XDefineCursor。我如何做與python綁定相同的?

我希望能夠使用默認光標或自定義光標。

回答

0

有你要記住,當你需要找到的python-Xlib的任何等價的libx11功能的兩件事情:

  1. 的libx11不同,蟒蛇,Xlib的是面向對象的;在這種情況下,XCreatePixmapCursor()轉換爲pixmap.create_cursor()
  2. 大多數python-xlib方法直接映射到X11消息,即。大部分幫手功能都沒有實現;如果找不到匹配的python-xlib方法,那麼通常需要查看libX11 source code以確定該函數是否只是一個幫助程序,用於調用其他函數。在這種情況下,如果你看看source code of 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()解釋。