Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement