python-3.x - Tkinter 在 Python 3 上找到但在 Python2.7 上找不到

标签 python-3.x python-2.7 tkinter

我在 CentOs7 上。我通过 yum 安装了 tk、tk-devel、tkinter。我可以在 Python 3 中导入 tkinter,但不能在 Python 2.7 中导入。有什么想法吗?

在 Python 3 (Anaconda) 中取得成功:

Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 13 2017, 12:02:49) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> 

但在 Python 2.7(CentOS 默认)上失败:

Python 2.7.5 (default, Aug  4 2017, 00:39:18) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/lib-tk/Tkinter.py", line 39, in <module>
  import _tkinter # If this fails your Python may not be configured for Tk
ImportError: libTix.so: cannot open shared object file: No such file or directory

我看了一些回答说

If it fails with "No module named _tkinter", your Python configuration needs to be modified to include this module (which is an extension module implemented in C). Do not edit Modules/Setup (it is out of date). You may have to install Tcl and Tk (when using RPM, install the -devel RPMs as well) and/or edit the setup.py script to point to the right locations where Tcl/Tk is installed. If you install Tcl/Tk in the default locations, simply rerunning "make" should build the _tkinter extension.

我已经通过yum重新安装了tk、tk-devel和tkinter,但问题还是一样。

如何配置它以在 Python 2.7 上运行?

最佳答案

对于 python 3 使用:

import tkinter

对于 python 2 使用:

import Tkinter

如果这些不适用于 python 3 安装:

sudo apt-get install python3-tk

或者,对于 python 2:

sudo apt-get install python-tk

您可以找到更多详细信息here

关于python-3.x - Tkinter 在 Python 3 上找到但在 Python2.7 上找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47972811/

相关文章:

ManytoMany 字段中的 Django ForeignKey 显示为 None

python - 如何在 OSX 终端中对所选文件应用 python 文件执行?

python - 如何调用另一个 lambda 异步并将上下文传递给它?

python - Numpy "fill"方法是如何实现的?

Python-如何从 tkinter 小部件获取值并将其分配给变量

python - 单击时 OptionMenu 不会显示第一个选项 (Tkinter)

excel - 使用 Python3 从大型 .xlsx 中高效提取工作表名称和列名称

python - 导入请求适用于 Windows shell 但不适用于 PyCharm

Python - 如何一键设置多个命令的优先级?

python - 将文本写入 gzip 文件