matplotlib导航栏错误'FigureCanvasTkAgg'对象在tkinter中没有属性'manager'

djp7away  于 2023-10-24  发布在  其他
关注(0)|答案(4)|浏览(305)

我尝试使用tkinter和matplotlib来创建一个接口来与一些实验室设备配对,但现在我只是加载一些旧的测试数据。我尝试在NavigationToolbar2Tk导航栏中添加。
当我运行程序时,工具栏会正常弹出,但是每次我点击其中一个按钮时,我都会得到错误“FigureCanvasTkAgg”对象没有属性“manager”。有趣的是,除了保存按钮外,所有按钮仍然会执行它们的操作,它们只是不断地吐出错误。我尝试为导航框创建一个单独的框架,但是没有成功。

import tkinter
import matplotlib
matplotlib.use('TkAgg')
from tkinter import Tk
from tkinter import Label as label
from tkinter import Message
from tkinter import Button as button
from tkinter import Canvas as canvas
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import pyplot as plt
from tkinter import Entry as entry
from matplotlib import style
from matplotlib.backends.backend_tkagg import NavigationToolbar2Tk
import url

dataset = url.open_and_convert_csv("Wednesday_4pm_107_2_Blue.csv")

data = dataset[2:]

x = []
y = []
for row in data:
    strain = row[3]
    x.append(strain)
    stress = row[4]
    y.append(stress)

plt.grid(True, which='major', axis='both')
plt.plot(x, y)
figure = plt.gcf()

def tensile_graph():

    canv.get_tk_widget().grid(column = 1, row = 1)

def title():
    title_text = title_box.get()
    title_box.delete(0,len(title_text))
    plt.title(title_text)
    figure = plt.gcf()
    canv = FigureCanvasTkAgg(figure, master=top)
    canv.get_tk_widget().grid(column=1, row=1)

def x_ax():
    x_ax_text = x_ax_box.get()
    x_ax_box.delete(0, len(x_ax_text))
    plt.xlabel(x_ax_text)
    figure = plt.gcf()
    canv = FigureCanvasTkAgg(figure, master=top)
    canv.get_tk_widget().grid(column=1, row=1)

def y_ax():
    y_ax_text = y_ax_box.get()
    y_ax_box.delete(0, len(y_ax_text))
    plt.ylabel(y_ax_text)
    figure = plt.gcf()
    canv = FigureCanvasTkAgg(figure, master=top)
    canv.get_tk_widget().grid(column=1, row=1)

top = tkinter.Tk()
top.geometry('1000x600+30+30')

canv = FigureCanvasTkAgg(figure, master=top)

tensile_graph()

options_frame = tkinter.Frame(top)
options_frame.grid(row = 1, column = 0)

title_box = entry(options_frame)
title_box.grid(row = 1, column = 0)
text = title_box.get()
title_button = button(options_frame,text='Apply',command = title)
title_button.grid(row = 1, column = 1)
title_label = label(options_frame,text='Title')
title_label.grid(row = 0, column = 0)

x_axlabel = label(options_frame,text='X Axis Label')
x_axlabel.grid(row = 2, column = 0)
x_ax_box = entry(options_frame)
x_ax_box.grid(row = 3, column = 0)
x_ax_button = button(options_frame, text = 'Apply', command = x_ax)
x_ax_button.grid(row = 3, column = 1)

y_axlabel = label(options_frame,text='Y Axis Label')
y_axlabel.grid(row = 4, column = 0)
y_ax_box = entry(options_frame)
y_ax_box.grid(row = 5, column = 0)
y_ax_button = button(options_frame, text = 'Apply', command = y_ax)
y_ax_button.grid(row = 5, column = 1)

toolbar_frame = tkinter.Frame(top)
toolbar_frame.grid(column = 1, row = 1)
toolbar = NavigationToolbar2Tk(canv,toolbar_frame)
toolbar.update()
canv._tkcanvas.grid(row=1, column=1)


top.mainloop()
ie3xauqp

ie3xauqp1#

我已经设法修复了这个错误。它实际上是NavigationToolbar2Tk类中的一个bug。如果你想修复它,你必须自己修改他们的代码。你只需要修改NavigationToolbar2Tk类中的一行代码。
1.导航到NavigationToolbar2Tk类。
1.导航到方法def set_cursor(self,cursor)。
1.将“window = self.canvas.manager.window”更改为“window = self.window”
你不会再看到错误了。

w9apscun

w9apscun2#

我也有同样的问题,而且当我把鼠标悬停在图上时,我不断地得到错误,我发现我必须降级我的matplotlib。下面是解决我的问题的版本:
Python 3.7.3,matplotlib 2.2.2
要降级你的matplotlib版本,打开提示符并写:

conda install matplotlib == 2.2.2
uqxowvwt

uqxowvwt3#

您的问题的答案可以通过查找问题How to get FigureCanvasTkAgg to work with Scrollbar来解决
似乎需要通过调用方法FigureCanvasTkAgg.get_tk_widget()来访问Tk小部件

self.canvas=FigureCanvasTkAgg(fig, master=self.frame)
self.canvas.get_tk_widget().config(bg='#FFFFFF',scrollregion=(0,0,500,500))
self.canvas.get_tk_widget().config(width=300,height=300)
self.canvas.get_tk_widget().config(xscrollcommand=self.hbar.set, yscrollcommand=self.vbar.set)
self.canvas.get_tk_widget().grid(row=0, column=0, sticky=W+E+N+S)
hfsqlsce

hfsqlsce4#

Blockquote我已经设法修复了这个错误。它实际上是NavigationToolbar2Tk类中的一个bug。如果你想修复它,你必须自己修改他们的代码。你只需要修改NavigationToolbar2Tk类中的一行代码。
Vincent的回答很有魅力。因为我不能直接对他的回答添加评论,所以我在这里添加要修改的文件:

<python_installation>\Lib\site-packages\matplotlib\backends\_backend_tk.py

相关问题