简体   繁体   English

Matplotlib backend_tkagg TclError

[英]Matplotlib backend_tkagg TclError

I installed Anaconda 3 (64-bit) Python 3.4 for Windows 7, and tried to test a sample from Matplotlib. 我为Windows 7安装了Anaconda 3(64位)Python 3.4,并尝试从Matplotlib中测试示例。 But when I ran the script, it came out with an exception like this: 但是当我运行脚本时,它出现了如下异常:

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

The code is from here , un-modified example: 代码来自这里 ,未经修改的示例:

#!/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()

It seems that tkagg.blit couldn't get the right renderer, so it raised an exception. 似乎tkagg.blit无法获得正确的渲染器,因此引发了异常。 And I couldn't find where self.renderer._renderer referred to. 而且我找不到self.renderer._renderer引用的位置。 Then I found a similar question from spyderlib Issue 1831: https://code.google.com/p/spyderlib/issues/detail?id=1831 . 然后我从spyderlib问题1831中找到了一个类似的问题: https : //code.google.com/p/spyderlib/issues/detail? id = 1831。

I guessed it was the problem of the Python 3.4 between Spyder, so I installed Anaconda (32-bit) Python 2.7 for Windows 7, and tried to run the sample script above in another Windows 7 system. 我猜想这是Spyder之间的Python 3.4的问题,因此我为Windows 7安装了Anaconda(32位)Python 2.7,并尝试在另一个Windows 7系统中运行上述示例脚本。 Then the tkinter GUI showed normally with a matplotlib figure, and no exception came out. 然后,tkinter GUI正常显示为matplotlib图形,没有异常出现。 So I was wondering maybe it's the problem of the Spyder version indeed. 所以我想知道这可能确实是Spyder版本的问题。 Our project is based on Python 3.4, and we don't want to move back to Python 2.7 because it's complicated to migrate. 我们的项目基于Python 3.4,我们不想移回Python 2.7,因为它迁移起来很复杂。 How do I solve this problem? 我该如何解决这个问题?

This is a bug in the Anaconda tk Matplotlib backend, which as far as I know it only affects Windows users. 这是Anaconda tk Matplotlib后端中的错误,据我所知,它仅影响Windows用户。

I let the Continuum guys know about it, but unfortunately they told me it's one of low priority for them because few people use the tk backend. 我让Continuum的人知道了这一点,但是不幸的是,他们告诉我这对他们来说不是一个优先事项,因为很少有人使用tk后端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM