我有一个奇怪的经验,将自定义鼠标光标设置为来自Gtk的pixbuf。下面的示例代码 - 此代码执行应有的操作(单击按钮时设置光标),但光标在设置时闪烁,就好像它被关闭并再次打开非常快。
#!/usr/bin/env python
"Grab the window cursor and set it to be a pixmap. This flickers; why?"
from gi.repository import Gtk, Gdk, GdkPixbuf, GLib
import cairo
def grab(button, window):
display = window.get_screen().get_display()
pointer = display.get_device_manager().get_client_pointer()
# Create a simple filled 100x100 rectangle as the cursor pixbuf
pb = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB, False, 8, 100, 100)
surface = Gdk.cairo_surface_create_from_pixbuf(pb, 0, None)
context = cairo.Context(surface)
context.set_source_rgba(1, 0.5, 0.5, 1)
context.rectangle(0, 0, 100, 100)
context.fill()
pbdrawn = Gdk.pixbuf_get_from_surface(surface, 0, 0, surface.get_width(), surface.get_height())
# Create the cursor, and grab the mouse and set that cursor
cursor = Gdk.Cursor.new_from_pixbuf(display, pbdrawn, 100, 100)
pointer.grab(
window.get_window(),
Gdk.GrabOwnership.NONE,
True,
Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.POINTER_MOTION_MASK | Gdk.EventMask.SCROLL_MASK,
cursor,
Gdk.CURRENT_TIME)
# After five seconds, quit
GLib.timeout_add(5000, ungrab, pointer)
def ungrab(pointer):
pointer.ungrab(Gdk.CURRENT_TIME)
w = Gtk.Window()
btn = Gtk.Button("Change cursor")
btn.connect("clicked", grab, w)
b = Gtk.Box()
b.pack_start(btn, True, True, 50)
w.add(b)
w.show_all()
w.connect("destroy", Gtk.main_quit)
Gtk.main()