Advertisement
Guest User

Flickering cursor with Gtk

a guest
May 13th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. #!/usr/bin/env python
  2. "Grab the window cursor and set it to be a pixmap. This flickers; why?"
  3. from gi.repository import Gtk, Gdk, GdkPixbuf, GLib
  4. import cairo
  5.  
  6. def grab(button, window):
  7.     display = window.get_screen().get_display()
  8.     pointer = display.get_device_manager().get_client_pointer()
  9.  
  10.     # Create a simple filled 100x100 rectangle as the cursor pixbuf
  11.     pb = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB, False, 8, 100, 100)
  12.     surface = Gdk.cairo_surface_create_from_pixbuf(pb, 0, None)
  13.     context = cairo.Context(surface)
  14.     context.set_source_rgba(1, 0.5, 0.5, 1)
  15.     context.rectangle(0, 0, 100, 100)
  16.     context.fill()
  17.     pbdrawn = Gdk.pixbuf_get_from_surface(surface, 0, 0, surface.get_width(), surface.get_height())
  18.  
  19.     # Create the cursor, and grab the mouse and set that cursor
  20.     cursor = Gdk.Cursor.new_from_pixbuf(display, pbdrawn, 100, 100)
  21.     pointer.grab(
  22.         window.get_window(),
  23.         Gdk.GrabOwnership.NONE,
  24.         True,
  25.         Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.POINTER_MOTION_MASK | Gdk.EventMask.SCROLL_MASK,
  26.         cursor,
  27.         Gdk.CURRENT_TIME)
  28.  
  29.     # After five seconds, quit
  30.     GLib.timeout_add(5000, ungrab, pointer)
  31.  
  32. def ungrab(pointer):
  33.     pointer.ungrab(Gdk.CURRENT_TIME)
  34.  
  35. w = Gtk.Window()
  36. btn = Gtk.Button("Change cursor")
  37. btn.connect("clicked", grab, w)
  38. b = Gtk.Box()
  39. b.pack_start(btn, True, True, 50)
  40. w.add(b)
  41. w.show_all()
  42. w.connect("destroy", Gtk.main_quit)
  43. Gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement