使用Tkinter和python3.5导入错误

时间:2016-09-21 18:09:16

标签: tkinter ubuntu-12.04 python-3.5

我无法使用python3.5运行matplotlib.pyplot和tkinter。我在Ubuntu 12.04.5 LTS上。我在输入dpkg -l python3-tk时看到安装了python3-tk。我还应该如何排除故障?

python3-tk只适用于python3.2而不是python3.5吗?

以下示例使用python3.2与python3.5

machine:~$ /usr/bin/python3
Python 3.2.3 (default, Jun 18 2015, 21:46:58)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> exit()

machine:~$ /usr/bin/python3.5
Python 3.5.2 (default, Jul 17 2016, 17:38:18)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
  File "/usr/lib/python3.5/tkinter/__init__.py", line 36, in <module>
    import _tkinter
ImportError: No module named '_tkinter'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/tkinter/__init__.py", line 38, in <module>
    raise ImportError(str(msg) + ', please install the python3-tk package')
ImportError: No module named '_tkinter', please install the python3-tk package

已安装python3-tk

machine:~$ dpkg -l python3-tk
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                         Version                      Description
    +++-============================-============================-

========================================================================
ii  python3-tk                   3.2.3-1                      Tkinter - Writing Tk applications with Python 3.x

4 个答案:

答案 0 :(得分:3)

sudo apt-get install python3-tk

此命令将仅为您的默认python3安装tkinter,因此python3.5 ImportError。

您可以运行以下命令来安装python3.5

sudo apt-get install python3.5-tk

答案 1 :(得分:2)

将matplotlib后端更改为'agg'或其他一些非交互选项。如下所示:

matplotlib.use( 'AGG')

More info here(在Docker的上下文中,我遇到了这个问题并成功解决了它):

答案 2 :(得分:0)

python3-tk库是在3.2版本下安装的,而不是3.5版本。卸载了Python3-tk并安装了python3.5-tk

答案 3 :(得分:0)

我尝试在ubuntu16.02中使用此命令,

sudo apt-get install python3.5-tk

默认情况下,我安装了python2.7。您可以参考此link