views:

390

answers:

1

It's likely that this is just a general Python Tkinter question, not necessarily a matplotlib one.

So I'm in the midst of developing a rather large suite of plotting functionality on top of matplotlib using the Matplotlib "TkAgg" backend (Agg rendering to a Tk canvas using TkInter). I'm using some of the default zooming functionality provided by matplotlib out of the box...specifically the "Zoom to box" button on the default matplotlib toolbar. I am creating my own toolbar by subclassing the existing "matplotlib.backends.backend_tkagg.NavigationToolbar2TkAgg" class.

Pretty much, the issue here is that I hate the default icon that "Zoom to box" uses (the Tkinter "tcross"). I've figured out how to use a different Tkinter built-in cursor (e.g. this changes the cursor to "plus" instead of "tcross"):

import matplotlib
matplotlib.use('TkAgg')

import matplotlib.backend_bases
import matplotlib.backends.backend_tk_agg

matplotlib.backends.backend_tkagg.cursord[matplotlib.backend_bases.cursors.SELECT_REGION] = "plus"

And in general, I know that to change the current mouse cursor to one of the built-in Tkinter ones from the toolbar class, I can just call:

self.window.configure(cursor="cursor_name")

So what I would really, really like is to be able to use a magnifying glass icon for when the user is in "zoom mode". I already have a .ppm of the magnifying glass icon I'd like to use and everything, but I can't figure out for the life of me how to use my magnifying glass as the mouse cursor icon. Is it possible to use a custom image as a mouse cursor in Python Tkinter? Help!

Platform note: This needs to be workable on Mac OS X 10.5+, RedHat Enterprise Linux 5, and possibly Solaris 10, so a platform-specific solution is undesirable.

+1  A: 

Something like this works with unix X11 XBM files:

import Tkinter
t = Tkinter.Tk()
t.configure(cursor=('@/usr/include/X11/bitmaps/star', '/usr/include/X11/bitmaps/starMask', 'black', 'white'))
t.mainloop()

As for the Macs, from the man page for "Tk_GetCursorFromData":

The Macintosh version of Tk supports all of the X cursors and will also accept any of the standard Mac cursors including ibeam, crosshair, watch, plus, and arrow. In addition, Tk will load Macintosh cursor resources of the types crsr (color) and CURS (black and white) by the name of the of the resource.
The application and all its open dynamic library's resource files will be searched for the named cursor. If there are conflicts color cursors will always be loaded in preference to black and white cursors.

Mark
@Mark thanks for the feedback. I guess I was just hoping there was an easier way to convert my existing PNG than to have to convert it to the X11 bitmap format (if you look at those files, they're actually C code with arrays of the pixels in the image). I'll give it a go and see if it does what I want.
Brent Nash
+1 for being the only viable answer in a week ;)
Brent Nash
@Brent, I just did a test with the always excellent ImageMagick's convert utility. It can do PNG to XBM conversion.
Mark
Thanks for the feedback. Sorry for the super-belated accept.
Brent Nash