简体   繁体   中英

Matplotlib backend_tkagg TclError

I installed Anaconda 3 (64-bit) Python 3.4 for Windows 7, and tried to test a sample from 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. And I couldn't find where self.renderer._renderer referred to. Then I found a similar question from spyderlib Issue 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. Then the tkinter GUI showed normally with a matplotlib figure, and no exception came out. So I was wondering maybe it's the problem of the Spyder version indeed. 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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