1

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()
6
  • Couldn't you use set_cursor() to set the cursor for the window? I've never done cursor work before, so I'm not experienced in it. Also, your timeout_add() may be causing some troubles. It takes careful planning to properly schedule events in a GTK program.
    – oldtechaa
    May 13, 2016 at 12:01
  • @oldtechaa I'm doing a pointer grab so that the cursor is always set to this while this bit of code is running. If I just do window.set_cursor() then it only sets it for my window.
    – sil
    May 13, 2016 at 12:13
  • An update: having asked some other people to test this, it seems to not be flickery for some people. So it may be a graphics driver issue rather than a Gtk issue.
    – sil
    May 14, 2016 at 19:52
  • Somewhere I read that Intel and nVidia tend to be faster than Radeons for Xlib drawing (Cairo's default backend). You don't happen to have AMD graphics, do you?
    – oldtechaa
    May 14, 2016 at 20:08
  • I don't; I have Intel. It has worked for a different Intel user and an AMD user, but not me.
    – sil
    May 14, 2016 at 20:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.