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

from matplotlib import pyplot

我得到以下错误:

ImportError: No module named 'tkinter'

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

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


当前回答

下载Python安装文件,单击“修改”,然后勾选“tcl/tk”并安装。

安装完成后,进入python安装文件夹(默认为C:\Users*您的用户名*\AppData\Local\Programs\ python \Python39\Lib)。

复制tkinter文件夹并将其粘贴到pycharm项目的lib文件夹中。

错误应该被解决

其他回答

我在Win x86/64上也遇到了同样的问题,因为我的自定义Python3.7安装不包括Tcl包,所以只需修改或重新安装python即可

https://www.python.org/downloads/release/python-370/

因为我在Ubuntu上使用Python 3.7,所以我必须使用:

sudo apt-get install python3.7-tk

Almost all answers I searched for this issue say that Python on Windows comes with tkinter and tcl already installed, and I had no luck trying to download or install them using pip, or actviestate.com site. I eventually found that when I was installing python using the binary installer, I had unchecked the module related to TCL and tkinter. So, I ran the binary installer again and chose to modify my python version by this time selecting this option. No need to do anything manually then. If you go to your python terminal, then the following commands should show you version of tkinter installed with your Python:

import tkinter
import _tkinter
tkinter._test()

在CentOS 6.5和python 2.7上,我需要做:yum安装python27-tkinter

也许你从源代码安装了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