python matplotlib.pyplot.imshow tkinter错误

时间:2018-03-12 17:05:14

标签: python tkinter ubuntu-16.04

无论是使用python2.7还是python3.5,都会出现此错误。 我想在硬盘上显示存储为文件的图像,所以我开始执行以下操作,并遇到此错误:

>>> 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.

事实上,经过一些研究后,检查问题是否存在的更简单方法就是执行以下操作:

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

就我而言,此时会出现同样的错误。

我想这可能是因为我最近将我的ubuntu发行版从14升级到16.04。 在此之前,'官方'python3版本是python3.4,但我还安装了python3.5,这是我现在需要使用的版本。 我尝试使用

卸载python3-tk软件包
sudo aptitude remove python3-tk

然后重新安装

sudo aptitude install python3-tk

但它没有解决问题。

我去了tkinter的网站http://www.tkdocs.com/tutorial/install.html,显然问题可能是因为python需要编译而知道确切的tck / tk版本可用。 所以我想我需要完全重新安装/编译python3.5。但我想知道如何在不破坏ubuntu发行版的情况下这样做,因为python3.5现在应该与16.04完全集成。有一种'简单'的方法吗?

顺便说一句,对于python2.7,情况更糟:

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

已经安装了'python-tk'软件包。我卸载它,然后重新安装它:

sudo aptitude remove python-tk

sudo aptitude install python-tk

但问题仍然存在。

请问有人知道有关这一切的事吗? Google对此并没有多大帮助,我担心......

1 个答案:

答案 0 :(得分:0)

好吧,最后我做了我在我的问题中提出的建议:从#34;官方&#34;安装一个新的python3.5。站点,编译它并根据http://www.tkdocs.com/tutorial/install.html详细说明,为我专门为此安装的tkinter版本指定位置。它奏效了。具体来说,我安装的版本是python3.5.5,而我以前的版本是python3.5.3。现在,当我输入&#34; python3.5&#34;召唤一个python终端,它是使用的3.5.5版本。适合我。