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

from matplotlib import pyplot

我得到以下错误:

ImportError: No module named 'tkinter'

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

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


当前回答

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

其他回答

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

sudo apt-get install python3.7-tk

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

pip install tkinter

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

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

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

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和必要的包。

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

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

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

错误应该被解决

有时(例如在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