【问题标题】:How do I get tkinter to work outside of IDLE?如何让 tkinter 在 IDLE 之外工作?
【发布时间】:2019-04-14 00:38:46
【问题描述】:

当我在 IDLE 中运行我的 python 脚本时,tkinter 工作正常,但是当我尝试直接通过 python 启动器或命令行运行我的脚本时,我会收到以下消息:

Traceback (most recent call last):
  File "/Users/maclaren/Documents/hellotkinter.py", line 1, in <module>
    import tkinter
ImportError: No module named tkinter

我在 mac 上编码,请帮忙。如何让我的程序在 IDE 之外运行?

【问题讨论】:

  • 很可能您在 IDE 中使用了特定的 python 安装,以及另一个(系统 python?)来运行您的代码。您能否提供有关您的 IDE 的更多详细信息,以及在系统终端或从您的 IDE 终端执行的which python 命令的结果是什么?
  • 您可能正在运行 Python 2.x,在这种情况下,模块被命名为 Tkinter
  • 我正在运行 python 3x 并且我正在使用 IDLE,我尝试使用 import tkinter 和 from tkinter import * 作为命令,它们都在 IDLE 中工作,但当我通过启动器运行它时却不行。我尝试使用最简单的软件来让我的代码正常工作(tkinter 和 IDLE),但我真的很沮丧。我首先通过编写一个简单的 hello world 进行了尝试
  • pip install tkinter...
  • 收集 tkinter 找不到满足 tkinter 要求的版本(来自版本:)没有为 tkinter 找到匹配的发行版

标签: python macos tkinter command-line


【解决方案1】:

您可能正在使用 IDLE python 3。在命令行中,python 2 是默认值。您应该从

更改导入
import tkinter

import Tkinter

或者您可以在命令行中使用 python 3。这是最好的选择,因为一些 Tk 小部件仅在 python 3 中可用。而不是这个:

cd /Users/maclaren/Documents
python hellotkinter.py

尝试:

cd /Users/maclaren/Documents
python3 hellotkinter.py

cd /Users/maclaren/Documents
py hellotkinter.py

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,如果您的程序在 IDE 之外无法运行,请在命令提示符或终端中打开您的程序,方法是编写 python myfile.pymyfile.py 是您正在尝试运行)。

    它会显示文件中的任何错误,而您的 IDE 有时不会显示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      • 2012-08-26
      • 2019-04-17
      • 1970-01-01
      • 2020-08-16
      相关资源
      最近更新 更多