Python 未找到 Tkinter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19030579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tkinter Not Found
提问by CodeMonkey
I'm running Windows 7 32-bit. I've installed Python 3.2.2 and selected every module for installation (including Tcl/Tk). On my computer, I can run a script by double-clicking the .py file and it will find my Tkinter import just fine. If I run it from a command line, it says ImportError: No module named 'Tkinter'
. I passed this script on to a coworker who also installed the same way, and she can't run the script at all even with double-clicking. Same Tkinter problem. Our PATHs are identical with C:\Python33 being the first item and tkinter shows in the lib folder. I'm running out of ideas. What's going on? Why is Tkinter so finicky with existing?
我正在运行 Windows 7 32 位。我已经安装了 Python 3.2.2 并选择了每个模块进行安装(包括 Tcl/Tk)。在我的计算机上,我可以通过双击 .py 文件来运行脚本,它会发现我的 Tkinter 导入很好。如果我从命令行运行它,它会显示ImportError: No module named 'Tkinter'
. 我将此脚本传递给了一个也以相同方式安装的同事,即使双击,她也根本无法运行该脚本。同样的 Tkinter 问题。我们的 PATH 与 C:\Python33 作为第一项和 tkinter 显示在 lib 文件夹中相同。我的想法不多了。这是怎么回事?为什么 Tkinter 对现有技术如此挑剔?
Update: Apparently Tcl/Tk do not include Tkinter. The reason it worked for me was because I had installed a special Python package via our company's download system that happened to include it. This version was linked to .py extensions. In command prompt, however, my updated Python (with Tcl/Tk but without Tkinter) was the python of choice as selected by my PATH variable. My coworker did not have this special package installed so it did not work for her. I had thought it was my Python 3.3 that was running the script but it was not which is why it seemed like it worked for me. That said, if anyone else runs into this issue, check out the sys.executable and sys.version as indicated below to figure out just what is going on!
更新:显然 Tcl/Tk 不包括 Tkinter。它对我有用的原因是我通过我们公司的下载系统安装了一个特殊的 Python 包,该包碰巧包含它。此版本链接到 .py 扩展名。然而,在命令提示符下,我更新的 Python(带有 Tcl/Tk 但没有 Tkinter)是我的 PATH 变量选择的 Python 选择。我的同事没有安装这个特殊的软件包,所以它对她不起作用。我原以为是我的 Python 3.3 正在运行脚本,但事实并非如此,这就是为什么它似乎对我有用。也就是说,如果其他人遇到此问题,请查看如下所示的 sys.executable 和 sys.version 以弄清楚发生了什么!
采纳答案by falsetru
You may have both Python 2.x and Python 3.x. And py
extension is linked to Python 2.x interpreter. And your python script is designed to run with Python 2.x.
您可能同时拥有 Python 2.x 和 Python 3.x。并且py
扩展链接到 Python 2.x 解释器。您的 Python 脚本旨在与 Python 2.x 一起运行。
In Python 3, Tkinter
module was renamed to tkinter
(lowercase).
在 Python 3 中,Tkinter
模块被重命名为tkinter
(小写)。
Make a script as follow, then run it by clicking it, and run it in command. You may get different results:
如下制作一个脚本,然后通过单击它来运行它,并在命令中运行它。你可能会得到不同的结果:
import sys
print(sys.version)
input()
回答by Ayush Raj
ImportError: No module named 'Tkinter'
In Python 3 Tkinter
is changed to tkinter
Try import tkinter as tk
ImportError: No module named 'Tkinter'
在 Python 3Tkinter
中改为tkinter
Tryimport tkinter as tk
Hope it helps!
希望能帮助到你!