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

from matplotlib import pyplot

我得到以下错误:

ImportError: No module named 'tkinter'

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

编辑:此外,我还尝试导入'tkinter'和'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

其他回答

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()

Linux

基于Debian的发行版:

sudo apt-get install python3-tk

基于RPM的发行版:

sudo yum install python3-tkinter

windows:

对于Windows,我认为问题是你没有安装完整的Python包。因为Tkinter应该与Python一起开箱发行。参见:http://www.tkdocs.com/tutorial/install.html。好的Windows python发行版可以在Anaconda或ActiveState公司找到。

测试python模块

python -c "import tkinter"

附注:我建议安装ipython,它提供了强大的shell和必要的包。

在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

如果你有pip(你可能有),在Windows上打开cmd或powershell,或者在OS X或Linux上打开终端窗口,然后尝试这样做(如果你在Windows上,请确保python在系统路径中):

pip install tkinter

安装tkinter需要一段时间,然后尝试执行以下代码块:

from tkinter import *
root = Tk()
# Your code goes here 
root.mainloop()

希望这能有所帮助!谢谢你!

如果你使用的是fedora,那么首先安装tkinter

sudo dnf install python3-tkinter

我认为你之后不需要导入tkinter 我也建议你使用virtualenv

$ python3 -m venv myvenv
$ source myvenv/bin/activate

并使用pip添加必要的包