Matplotlib ';AxesSubplot';对象在使用绘图重新打开Tkinter窗口后不可调用

Matplotlib ';AxesSubplot';对象在使用绘图重新打开Tkinter窗口后不可调用,matplotlib,tkinter,canvas,Matplotlib,Tkinter,Canvas,我创建了一个图形class。其对象的y值应在稍后阶段更新。应通过单击Tkinter按钮在新窗口中显示绘图。到目前为止,当单击按钮时,打开带有绘图的窗口是有效的。但一旦我用“绘图”关闭窗口并尝试重新打开窗口,就会出现以下错误: TypeError:“AxeSubPlot”对象不可调用 我使用以下代码: import tkinter from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg) from matplotlib.fi

我创建了一个图形
class
。其
对象的y值应在稍后阶段更新。应通过单击
Tkinter按钮
在新窗口中显示
绘图。到目前为止,当单击
按钮时,打开带有
绘图的窗口是有效的。但一旦我用“绘图”关闭窗口并尝试重新打开窗口,就会出现以下错误:

TypeError:“AxeSubPlot”对象不可调用

我使用以下代码:

import tkinter
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg)
from matplotlib.figure import Figure

root= tkinter.Tk()
root.geometry('800x800+50+50')
root.title('Spiel')

class Graph():
        
        def __init__(self, y):
            
            self.y = y
            
        def plot(self):
            self.window_graph = tkinter.Toplevel(root)
            self.window_graph.title("Graph")
            self.window_graph.geometry("300x300")
            
            self.fig = Figure(figsize=(5,5), dpi=55)
            self.plot = self.fig.add_subplot(111)
            self.plot.set_xlim([0,13])
            self.plot.set_ylim([0,400])
        
            #colors = ["red", "blue", "green", "yellow", "black"]
        
            self.line, = self.plot.plot(range(0,7),self.y,'r-', marker="o")
        
            self.plot.set_xlabel("Runde")
            self.plot.set_ylabel("Punkte")
            #self.plot.legend()
            self.canvas = FigureCanvasTkAgg(self.fig, master = self.window_graph)  
            self.canvas.draw()
            self.canvas.get_tk_widget().grid(row=0, column=0)
 
            
y = [1,2,3,4,5,6,7] 
graph_plot = Graph(y)

def plotting():
    graph_plot.plot()

tkinter.Button(root, text="Graph", command=plotting).pack()

root.mainloop()

有人对此有解决方案吗?

出现此问题是因为您正在设置
self.plot=self.fig.add\u子图(111)
。在Python中,这会覆盖该类的函数定义:

>>def func():
...     通过
...
>>>func
>>>func=“abc”
>>>func
“abc”
只需重命名函数或变量