matplotlib导航栏错误'FigureCanvasTkAgg'对象在tkinter中没有属性'manager' [英] matplotlib Navigation Bar error 'FigureCanvasTkAgg' object has no attribute 'manager' in tkinter

查看:37
本文介绍了matplotlib导航栏错误'FigureCanvasTkAgg'对象在tkinter中没有属性'manager'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 tkinter 和 matplotlib 创建一个接口以与一些实验室设备配对,但现在我只是加载一些旧的测试数据.我正在尝试在NavigationToolbar2Tk导航栏中添加.

I am trying to use tkinter and matplotlib to create an interface to pair with some lab equipment, but right now I am just loading in some old test data. I am trying to add in the NavigationToolbar2Tk navigation bar.

当我运行程序时,栏会正确弹出,但每次单击其中一个按钮时,我都会收到错误消息FigureCanvasTkAgg"对象没有属性manager".有趣的是,除保存外,所有按钮仍将执行其操作,只是不断吐出错误.我曾尝试为导航框创建一个单独的框架,但没有奏效.

When I run the program the bar pops up properly but every time I click one of the buttons I get the error 'FigureCanvasTkAgg' object has no attribute 'manager'. The funny thing is that all of the buttons except for save will still perform their operations, they just continually spit out the errors. I have tried creating a seperate frame for the navigation box but that hasn't worked.

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()

推荐答案

我已设法修复此错误.实际上,这是NavigationToolbar2Tk类中的错误.如果你想修复它,你必须自己修改他们的代码.您只需要在NavigationNavigatorbar2Tk类中更改一行代码.

I've managed to fix this error. It's actually a bug in NavigationToolbar2Tk class. If you want to fix it you have to modify their code yourself. You only need to change one line of code in the class NavigationToolbar2Tk.

  1. 导航到 NavigationToolbar2Tk 类.
  2. 导航到方法 def set_cursor(self, cursor).
  3. 更改window = self.canvas.manager.window"到"window = self.window"

您不会再看到错误.

这篇关于matplotlib导航栏错误'FigureCanvasTkAgg'对象在tkinter中没有属性'manager'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆