TclError:无法调用“canvas”命令:应用程序已被破坏

时间:2014-09-18 01:42:58

标签: python python-2.7 matplotlib tkinter

我正在尝试从控制台运行一些代码,但是我得到了TclError。以下是整个追溯:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
    return self.func(*args)
  File "SM_analyser.py", line 446, in OnB_maxq
    self.canvas = FigureCanvasTkAgg(self.plotter, self)
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 225, in __init__
    master=master, width=w, height=h, borderwidth=4)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2195, in __init__
    Widget.__init__(self, master, 'canvas', cnf, kw)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2055, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
TclError: can't invoke "canvas" command:  application has been destroyed

我有理由相信以下代码块是负责任的。它应该将图形'figure'添加到tkinter画布中,但是当我运行它时,'figure'被绘制在一个单独的窗口中并且给出了Tcl错误。

self.plotter = plt.figure('figure')            
plt.contour(array, linewidths = 1, colors = 'k')
plt.contourf(array, cmap = plt.cm.jet)
plt.ylabel('Y', fontdict = {'fontsize':16})
plt.xlabel('A', fontdict = {'fontsize':16})
plt.colorbar()
plt.title('figure', fontdict = {'fontsize':20})
plt.show()       
self.canvas = FigureCanvasTkAgg(self.plotter, self)
self.canvas.get_tk_widget().grid(column=14,row=2,rowspan=34)
plt.close()
self.canvas._tkcanvas.config(highlightthickness=0)

1 个答案:

答案 0 :(得分:0)

TclError: can't invoke "canvas" command: application has been destroyed表示您正在尝试创建Canvas类的实例,但该应用程序的主窗口不再存在。您可能希望逐步执行程序的逻辑,以查看在创建画布之前是否要在某个时刻销毁根窗口。