0

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?

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

4 Comments

it works in ubuntu 22.04.1, python -v3.10.4 but doesn't seem to work in windows 7, python -v3.8.1 and sadly I need it work in the later one
Do you have any error message? What is the matplotlib version you use in W7?
nope there isn't any error message, matplotlib -v3.1.3 in w7
The problem is, that you're using older version of matplotlib. I have updated my code, so it's working with both versions. You should anyway update your version to more recent to avoid future compatibility issues.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.