Skip to content

Gdk.Cursor

class — extends GObject.Object

Used to create and destroy cursors.

Cursors are immutable objects, so once you created them, there is no way to modify them later. You should create a new cursor when you want to change something about it.

Cursors by themselves are not very interesting: they must be bound to a window for users to see them. This is done with Surface.set_cursor or Surface.set_device_cursor. Applications will typically use higher-level GTK functions such as Gtk.Widget.set_cursor instead.

Cursors are not bound to a given Display, so they can be shared. However, the appearance of cursors may vary when used on different platforms.

Named and texture cursors

There are multiple ways to create cursors. The platform's own cursors can be created with Cursor.new_from_name. That function lists the commonly available names that are shared with the CSS specification. Other names may be available, depending on the platform in use. On some platforms, what images are used for named cursors may be influenced by the cursor theme.

Another option to create a cursor is to use Cursor.new_from_texture and provide an image to use for the cursor.

To ease work with unsupported cursors, a fallback cursor can be provided. If a Surface cannot use a cursor because of the reasons mentioned above, it will try the fallback cursor. Fallback cursors can themselves have fallback cursors again, so it is possible to provide a chain of progressively easier to support cursors. If none of the provided cursors can be supported, the default cursor will be the ultimate fallback.

Constructors

new_from_callback

@classmethod
def new_from_callback(cls, callback: CursorGetTextureCallback, fallback: Cursor | None = ...) -> Cursor | None

Creates a new callback-based cursor object.

Cursors of this kind produce textures for the cursor image on demand, when the callback is called.

Parameters:

  • callback — the GdkCursorGetTextureCallback
  • fallback — the GdkCursor to fall back to when this one cannot be supported

new_from_name

@classmethod
def new_from_name(cls, name: str, fallback: Cursor | None = ...) -> Cursor | None

Creates a new cursor by looking up name in the current cursor theme.

A recommended set of cursor names that will work across different platforms can be found in the CSS specification:

"none" No cursor
"default" The default cursor
"help" Help is available
"pointer" Indicates a link or interactive element
"context-menu" A context menu is available
"progress" Progress indicator
"wait" Busy cursor
"cell" Cell(s) may be selected
"crosshair" Simple crosshair
"text" Text may be selected
"vertical-text" Vertical text may be selected
"alias" DND: Something will be linked
"copy" DND: Something will be copied
"move" DND: Something will be moved
"dnd-ask" DND: User can choose action to be carried out
"no-drop" DND: Can't drop here
"not-allowed" DND: Action will not be carried out
"grab" DND: Something can be grabbed
"grabbing" DND: Something is being grabbed
"n-resize" Resizing: Move north border
"e-resize" Resizing: Move east border
"s-resize" Resizing: Move south border
"w-resize" Resizing: Move west border
"ne-resize" Resizing: Move north-east corner
"nw-resize" Resizing: Move north-west corner
"sw-resize" Resizing: Move south-west corner
"se-resize" Resizing: Move south-east corner
"col-resize" Resizing: Move an item or border horizontally
"row-resize" Resizing: Move an item or border vertically
"ew-resize" Moving: Something can be moved horizontally
"ns-resize" Moving: Something can be moved vertically
"nesw-resize" Moving: Something can be moved diagonally, north-east to south-west
"nwse-resize" Moving: something can be moved diagonally, north-west to south-east
"all-resize" Moving: Something can be moved in any direction
"all-scroll" Can scroll in any direction
"zoom-in" Zoom in
"zoom-out" Zoom out

Parameters:

  • name — the name of the cursor
  • fallbackNone or the GdkCursor to fall back to when this one cannot be supported

new_from_texture

@classmethod
def new_from_texture(cls, texture: Texture, hotspot_x: int, hotspot_y: int, fallback: Cursor | None = ...) -> Cursor

Creates a new cursor from a GdkTexture.

Parameters:

  • texture — the texture providing the pixel data
  • hotspot_x — the horizontal offset of the “hotspot” of the cursor
  • hotspot_y — the vertical offset of the “hotspot” of the cursor
  • fallback — the GdkCursor to fall back to when this one cannot be supported

Methods

get_fallback

def get_fallback(self) -> Cursor | None

Returns the fallback for this cursor.

The fallback will be used if this cursor is not available on a given GdkDisplay. For named cursors, this can happen when using nonstandard names or when using an incomplete cursor theme. For textured cursors, this can happen when the texture is too large or when the GdkDisplay it is used on does not support textured cursors.

get_hotspot_x

def get_hotspot_x(self) -> int

Returns the horizontal offset of the hotspot.

The hotspot indicates the pixel that will be directly above the cursor.

Note that named cursors may have a nonzero hotspot, but this function will only return the hotspot position for cursors created with Cursor.new_from_texture.

get_hotspot_y

def get_hotspot_y(self) -> int

Returns the vertical offset of the hotspot.

The hotspot indicates the pixel that will be directly above the cursor.

Note that named cursors may have a nonzero hotspot, but this function will only return the hotspot position for cursors created with Cursor.new_from_texture.

get_name

def get_name(self) -> str | None

Returns the name of the cursor.

If the cursor is not a named cursor, None will be returned.

get_texture

def get_texture(self) -> Texture | None

Returns the texture for the cursor.

If the cursor is a named cursor, None will be returned.

Properties

fallback

fallback: Cursor  # read/write

Cursor to fall back to if this cursor cannot be displayed.

hotspot_x

hotspot_x: int  # read/write

X position of the cursor hotspot in the cursor image.

hotspot_y

hotspot_y: int  # read/write

Y position of the cursor hotspot in the cursor image.

name

name: str  # read/write

Name of this this cursor.

The name will be None if the cursor was created from a texture.

texture

texture: Texture  # read/write

The texture displayed by this cursor.

The texture will be None if the cursor was created from a name.