Home > Software design >  How to keep default cursor in matplotlib?
How to keep default cursor in matplotlib?

Time:08-30

I'm using matplotlib with tkinter:

from tkinter import *
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk

root = Tk()

graph_frame=Frame(root,bg="red",height=10,width=10)
graph_frame.pack(side=TOP)

fig = Figure(figsize=(5, 4), dpi=100)
a=fig.add_subplot(1,1,1)

x = [1,3,12,15,1]
y = [10,9,8,55,19]

a.plot(x,y)

canvas = FigureCanvasTkAgg(fig, master=graph_frame)  # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=TOP, expand=1)

toolbar = NavigationToolbar2Tk(canvas, graph_frame) # toolbar
toolbar.update()
canvas.get_tk_widget().pack(side=TOP, expand=1)

root.mainloop()

I want the cursor to stay in default mode instead of magnifying glass cursor or moving cross cursor when clicking on zoom or pan respectively

How do I achieve this?

CodePudding user response:

You can override set_cursor method of your canvas.
Mouse cursor won't change for any tool.

EDIT: For older version of matplotlib, toolbar.set_cursor must be override as well.

Here is your modified code:

from tkinter import *

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure


def set_cursor_custom(cursor):
    """Ignore any cursor changes"""
    pass


root = Tk()

graph_frame = Frame(root, bg="red", height=10, width=10)
graph_frame.pack(side=TOP)

fig = Figure(figsize=(5, 4), dpi=100)
a = fig.add_subplot(1, 1, 1)

x = [1, 3, 12, 15, 1]
y = [10, 9, 8, 55, 19]

a.plot(x, y)

canvas = FigureCanvasTkAgg(fig, master=graph_frame)  # A tk.DrawingArea.
# Override set_cursor
canvas.set_cursor = set_cursor_custom

canvas.draw()
canvas.get_tk_widget().pack(side=TOP, expand=1)

toolbar = NavigationToolbar2Tk(canvas, graph_frame)  # toolbar

# Override set_cursor
toolbar.set_cursor = set_cursor_custom
toolbar.update()
canvas.get_tk_widget().pack(side=TOP, expand=1)

root.mainloop()
  • Related