Is it possible to make a custom mouse cursor with Python Tkinter? (Using matplotlib with the TkAgg backend)
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 i开发者_StackOverflowmage 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.
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.
精彩评论