我在 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/