How can I change cursors

The cursor is the most direct avatar of the user's presence on the screen. As such, it's used to provide immediate feedback to the user about the area being explored. Normally, it's a pointer to where the user's next mouse click will take effect, but depending on what widget the cursor is currently over, it can be a directional arrow, a text placement I-beam, or crosshairs. Typically, an application signals that it is too busy to accept input by changing the cursor to some kind of busy symbol, such as the classic hourglass.

You'll want to use the cursor to signal state in your application, and even though wxPython handles most of the basics for common widgets, you'll sometimes want to customize it, either because you have a custom widget or because your application needs to override the normal cursor behavior to signal a new state. Perhaps you just want your application to have its own unique cursor. In this section we'll show you how to use a cursor to signal state and how to customize cursors in wxPython.

You can create cursor objects in a variety of ways in wxPython. The cursor class is wx.Cursor, and there are two different ways of creating an instance. The simpler is the method wx.StockCursor(id), that returns an instance of a predefined system cursor. As defined in table 12.3, the id parameter can be any number of flags.

Table 12.3 Predefined stock cursors

Cursor ID

Description

wx.CURSOR_ARROW

The normal arrow cursor.

wx.CURSOR_ARROWWAIT

A busy cursor showing both the normal arrow and an hourglass. Available on Windows systems only.

wx.CURSOR_BLANK

The invisible cursor. Useful when you want to trick the user.

wx.CURSOR_BULLSEYE

A bullseye cursor (small circle inside a larger cursor). Sometimes useful for precision pointing.

wx.CURSOR_CHAR

A character cursor. Not available on all platforms.

wx.CURSOR_CROSS

The ever-popular cross-hair cursor.

wx.CURSOR_HAND

Classic pointing hand cursor.

wx.CURSOR_IBEAM

The vertical I-beam cursor typically used inside a text edit field.

wx.CURSOR_LEFT_BUTTON

A cursor that depicts a mouse with the left button pressed—used to hint to the user that he should press that button. May not be available on all platforms.

wx.CURSOR_MAGNIFIER

Magnifying glass typically used to indicate zoom.

wx.CURSOR_MIDDLE_BUTTON

Depicts a mouse with the middle button pressed.

wx.CURSOR_NO_ENTRY

Cursor containing the arrow and a circle-with-a-slash no entry symbol. Used to indicate that an area of the screen is invalid (as the target of a drag and drop, for example).

wx.CURSOR_PAINT_BRUSH

A cursor that looks like a paint brush. Typically used in a paint program.

wx.CURSOR_PENCIL

The pencil cursor usually used in draw programs.

wx.CURSOR_POINT_LEFT

The cursor is a left-pointing arrow.

wx.CURSOR_POINT_RIGHT

The cursor is a right-pointing arrow.

wx.CURSOR_QUESTION_ARROW

The arrow with the question mark next to it usually used to indicate context-sensitive help.

wx.CURSOR_RIGHT_ARROW

Just like the normal arrow cursor, but in a mirror, so that it points to the right.

wx.CURSOR_RIGHT_BUTTON

A mouse with the right button pressed.

Table 12.3 Predefined stock cursors (continued)

Cursor ID

Description

wx.CURSOR_SIZENESW

One of the cursors usually used to indicate both direction resize, this one has arrows pointing from the lower left (southwest) to upper right (northeast).

wx.CURSOR_SIZENS

The vertical resize cursor, pointing up and down.

wx.CURSOR_SIZENWSE

The other dual-direction resize cursor, pointing from lower right to upper left.

wx.CURSOR_SIZEWE

The horizontal reize cursor, pointing left and right.

wx.CURSOR_SIZING

Generic sizing cursor, with arrows pointing in all four compass directions.

wx. C U RSO R_S P RAYCA N

Another painting cursor.

wx.CURSOR_WAIT

The hourglass wait cursor.

wx.CURSOR_WATCH

The watch-shaped wait cursor.

Now that we've discussed predefined cursors, we'll show you how to create custom cursors using your own images.

Creating custom cursors

You can create a custom cursor from an image using the method wx.CursorFrom-Image(image). The image parameter is a wx.lmage instance. The image is automatically resized to 32 x 32 (on MacOS 16 x 16). The mask color of the image becomes the transparent color of the cursor. By default, the hotspot is set to the (0,0) corner of the cursor image. To change that, you need to set an option in the image before you convert it:

image.SetOptionInt(wx.IMAGE_OPTION_CUR_HOTSPOT_X, 0) image.SetOptionInt(wx.IMAGE_OPTION_CUR_HOTSPOT_Y, 22)

The actual wx.Cursor constructor is:

wx.Cursor(name, type, hotSpotX=0, hotSpotY=0)

The name parameter is the filename of the cursor to load. The type parameter is the icon type to load and uses the same wx.BlTMAP_TYPE_ flags as wx.lmage and wx.Bitmap. The hotSpotX and hotSpotY parameters set the hotspot of the icon. If the bitmap type used is wx.bitmap_type_cur to load from a .cur file, then the hotspot is set automatically from the hotspot defined in the .cur file.

Once you have the cursor created, using it is a simple matter of calling the SetCursor(cursor) on any wxPython widget. This method causes the cursor to be changed to the new shape when the cursor passes over the widget. There is also a global wx.SetCursor(cursor) method that globally sets the cursor for the entire application.

Images are great, but they only scratch the surface of what you can do graphically in wxPython. In the next section, we'll show how to send draw commands to the screen, or a file, or other display objects.

Was this article helpful?

+2 -3

Responses

  • violetta
    How to show hand cursor on text aream python tkinter simply?
    4 years ago
  • fiori
    How to change the cursor in python?
    8 months ago
  • hannele
    How to change the mouse cursor on python?
    3 months ago
  • arran
    How to change mouse cursor in wxpython floatcanvas?
    2 months ago

Post a comment