wxPython: Messing with Mouse Cursors

Lately on the wxPython mailing list, there's been a fair amount of traffic on changing the mouse icon. In this article I will describe different ways to manipulate the cursor with wxPython. To follow along, I recommend that you download Python 2.4 or higher and wxPython 2.8.x.

One of the cool things about wxPython (and Python in general) is that it has "batteries included". In this case, wxPython provides a list of available stock cursors in the wxPython docs. Here's how you would set a stock icon:

myCursor= wx.StockCursor(wx.CURSOR_POINT_LEFT)
myFrame.SetCursor(myCursor)

Of course, sometimes you may want to use a custom cursor. If that's the case, then you can create a cursor object using wx.Cursor(). Here's how I would do it in Windows. You'll have to adjust as needed for your OS.


# wx.Cursor(path\to\file, wx.BITMAP_TYPE* constant)
myCursor= wx.Cursor(r"C:\WINDOWS\Cursors\3dgarro.cur",
                    wx.BITMAP_TYPE_CUR)

# self is a wx.Frame in my example
myFrame.SetCursor(myCursor)

Note that you may not be able to use custom cursors on Mac. At least, that's what Robin Dunn said on his blog. That's all you really need to know to make this work. If you have questions, be sure to email me at "mike at pythonlibrary dot org" or the wxPython user's group.

Copyright © 2024 Mouse Vs Python | Powered by Pythonlibrary