【发布时间】:2014-10-08 22:03:17
【问题描述】:
我为 Windows 7 安装了 Anaconda 3(64 位)Python 3.4,并尝试测试来自 Matplotlib 的样本。但是当我运行脚本时,它出现了这样的异常:
Traceback (most recent call last):
File "<ipython-input-7-7482c0850da6>", line 1, in <module>
runfile('E:/Kanbox/Python/HWV/test/matplotlib_test.py', wdir='E:/Kanbox/Python/HWV/test')
File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 585, in runfile
execfile(filename, namespace)
File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 48, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "E:/Kanbox/Python/HWV/test/matplotlib_test.py", line 36, in <module>
canvas.show()
File "C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 349, in draw
tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
File "C:\Anaconda3\lib\site-packages\matplotlib\backends\tkagg.py", line 20, in blit
tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode, id(bbox_array))
TclError
代码来自here,未修改示例:
#!/usr/bin/env python
import matplotlib
matplotlib.use('TkAgg')
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
def destroy(e): sys.exit()
root = Tk.Tk()
root.wm_title("Embedding in TK")
root.bind("<Destroy>", destroy)
f = Figure(figsize=(5,4), dpi=100)
a = f.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)
a.plot(t,s)
a.set_title('Tk embedding')
a.set_xlabel('X axis label')
a.set_ylabel('Y label')
# A tk.DrawingArea
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
#toolbar = NavigationToolbar2TkAgg(canvas, root)
#toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
button = Tk.Button(master=root, text='Quit', command=sys.exit)
button.pack(side=Tk.BOTTOM)
Tk.mainloop()
tkagg.blit 似乎无法获得正确的渲染器,因此引发了异常。而且我找不到 self.renderer._renderer 所指的位置。然后我从 spyderlib Issue 1831 中找到了一个类似的问题:https://code.google.com/p/spyderlib/issues/detail?id=1831。
我猜是 Spyder 之间的 Python 3.4 的问题,所以我安装了适用于 Windows 7 的 Anaconda(32 位)Python 2.7,并尝试在另一个 Windows 7 系统中运行上面的示例脚本。然后tkinter GUI正常显示matplotlib图,没有出现异常。所以我想知道也许这确实是 Spyder 版本的问题。我们的项目基于 Python 3.4,我们不想回到 Python 2.7,因为迁移起来很复杂。我该如何解决这个问题?
【问题讨论】:
-
是未修改的示例 (matplotlib.org/examples/user_interfaces/embedding_in_tk.html) 吗?如果不是,该示例是否正确运行?如果是这样,您应该修改您的问题以反映这不是您的代码。
-
是的,这是您发布的链接中未修改的示例,感谢您的建议,我已经修改了我的问题。
标签: python matplotlib spyder