我尝试在windows 10上通过Pycharm IDE使用matplotlib包。 当我运行这段代码时:

from matplotlib import pyplot

我得到以下错误:

ImportError: No module named 'tkinter'

我知道在python2中。它被称为Tkinter,但这不是问题-我只是安装了一个全新的python 3.5.1。

编辑:此外,我还尝试导入'tkinter'和'tkinter' -这两个都不工作(都返回了我提到的错误消息)。


当前回答

有时(例如在osgeo4w分布中)tkinter被删除。

尝试更改matplotlib后端编辑位于[python install dir]/matplotlib/mpl-data/matplotlibrc中的matplotlibrc文件,将后端参数从backend: TkAgg更改为其他诸如backend: Qt4Aggas的内容,此处描述:http://matplotlib.org/faq/usage_faq.html#what-is-a-backend

其他回答

对于windows用户,重新运行安装程序。选择修改。选中tcl/tk和IDLE复选框。它的描述是“安装tkinter”

在CentOS 7和Python 3.4上,命令为sudo yum install python34-tkinter

在Redhat 7.4和Python 3.6上,命令是sudo yum install rh-python36-python-tkinter

在2018年初的Ubuntu上,Ubuntu (xenial/16.04)的正态分布中没有python3.6-tk,所以即使你有早期版本的python-tk也不能工作。

我的解决方案是使用python 3.5设置所有内容:

 sudo apt install python3.5-tk
 virtualenv --python=`which python3.5` python-env
 source python-env/bin/activate
 pip install -r requirements.txt

现在matplotlib可以找到tkinter。

编辑:

毕竟我只需要3.6个,诀窍是:

sudo apt install tk-dev

然后在tk-dev之后重新构建python3.6,例如:

./configure
make
make install

也许你从源代码安装了python。在这种情况下,您可以重新编译支持tcl/tk的python。

从http://www.tcl.tk/software/tcltk/download.html编译并安装tcl/tk,我假设你在/home/xxx/local/tcl-tk/安装python。

# install tcl
wget -c https://prdownloads.sourceforge.net/tcl/tcl8.6.9-src.tar.gz
tar -xvzf tcl8.6.9-src.tar.gz
cd tcl8.6.9
./configure --prefix=/home/xxx/local/tcl-tk/
make
make install

# install tk
wget -c https://prdownloads.sourceforge.net/tcl/tk8.6.9.1-src.tar.gz
tar -xvzf tk8.6.9.1-src.tar.gz
cd tk8.6.9.1
./configure --prefix=/home/xxx/local/tcl-tk/
make
make install

使用tcl/tk重新编译python,例如:

# download the source code of python and decompress it first.

cd <your-python-src-dir>
./configure --prefix=/home/xxx/local/python \
 --with-tcltk-includes=/home/xxx/local/tcl-tk/include \
 --with-tcltk-libs=/home/xxx/local/tcl-tk/lib
make 
make install

按照以下步骤在PyCharm IDE上轻松安装Tkinter:

首先进入文件:

其次是进入新项目设置>设置新项目:

然后点击新项目的设置,你会被重定向到这里:

请点击这里的+符号:

在安装future之后,你就可以……