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

from matplotlib import pyplot

我得到以下错误:

ImportError: No module named 'tkinter'

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

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


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


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

sudo yum install tkinter

解决问题。


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


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

sudo dnf install python3-tkinter

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

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

并使用pip添加必要的包


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 7和Python 3.4上,命令为sudo yum install python34-tkinter

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


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


如果你使用的是python 3.6,这对我来说是有效的:

sudo apt-get install python3.6-tk

而不是

sudo apt-get install python3-tk

哪个适用于其他版本的python3


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


在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

对于Windows用户,无需再次下载安装程序。只需要做以下几点:

进入开始菜单,输入应用程序和功能, 在搜索框中搜索“python”, 选择Python版本(例如Python 3.8.3rc1(32位))并单击修改, 在“修改设置”页面单击“修改”, 勾选td/tk和IDLE复选框(这会安装tkinter),然后单击下一步。

等待安装,您就完成了。


你可以使用

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

如果你根本不想使用tkinter。

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

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


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

sudo apt-get install python3.7-tk

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

为像我这样的穷人使用python 3.7。您需要python3.7-tk包。

安装python3.7-tk

$ python
Python 3.7.4 (default, Sep  2 2019, 20:44:09)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tkinter'
>>> exit()

请注意。安装Python3-tk。但不是python3.7-tk。

$ sudo apt install python3.7-tk
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
  tix python3.7-tk-dbg
The following NEW packages will be installed:
  python3.7-tk
0 upgraded, 1 newly installed, 0 to remove and 34 not upgraded.
Need to get 143 kB of archives.
After this operation, 534 kB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu xenial/main amd64 python3.7-tk amd64 3.7.4-1+xenial2 [143
kB]
Fetched 143 kB in 0s (364 kB/s)
Selecting previously unselected package python3.7-tk:amd64.
(Reading database ... 256375 files and directories currently installed.)
Preparing to unpack .../python3.7-tk_3.7.4-1+xenial2_amd64.deb ...
Unpacking python3.7-tk:amd64 (3.7.4-1+xenial2) ...
Setting up python3.7-tk:amd64 (3.7.4-1+xenial2) ...

安装之后,一切都好了。

$ python3
Python 3.7.4 (default, Sep  2 2019, 20:44:09)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> exit()

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

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


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

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

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

错误应该被解决


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

pip install tkinter

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

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

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


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

首先进入文件:

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

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

请点击这里的+符号:

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