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

from matplotlib import pyplot

我得到以下错误:

ImportError: No module named 'tkinter'

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

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


当前回答

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

其他回答

在Centos上,包名和命令是不同的。你需要做的是:

sudo yum install tkinter

解决问题。

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

sudo apt-get install python3.7-tk

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

首先进入文件:

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

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

请点击这里的+符号:

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

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

你可以使用

import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt

如果你根本不想使用tkinter。

此外,如果使用笔记本,不要忘记在笔记本顶部使用%matplotlib inline。

编辑:agg是一个不同的后端像tkinter为matplotlib。