I'm having a weird experience with setting a custom mouse cursor as a pixbuf from Gtk. Example code below -- this code does what it should (set the cursor when the button is clicked), but the cursor flickers while it's set as though it's being turned off and on again very fast.
#!/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()
set_cursor()to set the cursor for the window? I've never done cursor work before, so I'm not experienced in it. Also, yourtimeout_add()may be causing some troubles. It takes careful planning to properly schedule events in a GTK program.window.set_cursor()then it only sets it for my window.