简体   繁体   中英

python matplotlib.pyplot.imshow tkinter error

I have this error that occurs whether I am using python2.7 or python3.5. I want to display an image stored as a file on my hard drive, so I start to do the following, and encounter this error:

>>> import numpy
>>> import PIL
>>> import matplotlib.pyplot as plt
>>> image = PIL.Image.open("1000_NFI-04710047.png")
>>> a = numpy.array(image)
>>> plt.imshow(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/pyplot.py", line 3142, in imshow
    ax = gca()
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/pyplot.py", line 950, in gca
    return gcf().gca(**kwargs)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/pyplot.py", line 586, in gcf
    return figure()
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/pyplot.py", line 535, in figure
    **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_tkagg.py", line 81, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_tkagg.py", line 89, in new_figure_manager_given_figure
    window = Tk.Tk()
  File "/usr/lib/python3.5/tkinter/__init__.py", line 1880, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: Can't find a usable tk.tcl in the following directories: 
    /usr/local/lib/tcl8.6/tk8.6 /usr/local/lib/tk8.6 /usr/lib/tk8.6 /lib/tk8.6 /usr/library

/usr/local/lib/tk8.6/tk.tcl: version conflict for package "Tk": have 8.6.5, need exactly 8.6.1
version conflict for package "Tk": have 8.6.5, need exactly 8.6.1
    while executing
"package require -exact Tk  8.6.1"
    (file "/usr/local/lib/tk8.6/tk.tcl" line 16)
    invoked from within
"source /usr/local/lib/tk8.6/tk.tcl"
    ("uplevel" body line 1)
    invoked from within
"uplevel #0 [list source $file]"


This probably means that tk wasn't installed properly.

In fact, after a bit of research, an easier way to check whether or not the problem exists is just to do the following :

>>>> import tkinter
>>>> tkinter._test()

In my case, the same error appears at this point.

I think it may be because I have recently upgraded my ubuntu distribution from 14 to 16.04. Before that, the 'official' python3 version was python3.4, but I had also installed python3.5, which is the version I need to use right now. I tried uninstalling the python3-tk package, with

sudo aptitude remove python3-tk

and then installing it anew with

sudo aptitude install python3-tk

but it does not solve the problem.

I went on the site of tkinter, http://www.tkdocs.com/tutorial/install.html , and apparently the problem may be caused because python needs to be compiled while knowing the exact tck/tk version that will be available for it. So I guess I would need to completely reinstall / compile python3.5. But I would like to know how to do so without breaking the ubuntu distribution, since python3.5 is now supposed to fully integrated with 16.04. Is there an 'easy' way to do that?

By the way, for python2.7 it is even worse:

Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named tkinter

The package 'python-tk' was already installed. I uninstalled it, and then installed it anew with:

sudo aptitude remove python-tk

and

sudo aptitude install python-tk

but the problem persists.

Would someone know something to do about all this, please? Google was not much help for me for this, I'm afraid...

Well, in the end I did what I suggested in my question: install anew python3.5 from the "official" site, compiling it and specifying the location to a version of tkinter that I installed specifically for this, following the instruction detailed at http://www.tkdocs.com/tutorial/install.html . And it worked. Specifically, the version I installed was python3.5.5, whereas the previous version I had was python3.5.3. It seems that, now, when I type "python3.5" to summon a python terminal, it is the 3.5.5 version that is used. Works for me.

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