Python 如何让tkinter在空闲时间之外工作?

Python 如何让tkinter在空闲时间之外工作?,python,macos,tkinter,command-line,Python,Macos,Tkinter,Command Line,当我在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 回溯(最近一次呼叫

当我在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
回溯(最近一次呼叫最后一次):
文件“/Users/maclaren/Documents/hellotkinter.py”,第1行,在
进口tkinter
ImportError:没有名为tkinter的模块

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

您可能正在使用空闲的python 3。在命令行中,python 2是默认值。您应该将导入更改为

import tkinter

或者可以在命令行中使用python 3。这是最好的选择,因为一些Tk小部件仅在Python3中可用。与此相反:

cd /Users/maclaren/Documents
python hellotkinter.py
尝试:


我也遇到了同样的问题,如果您的程序不能在IDE之外工作,请通过编写
python myfile.py
(myfile.py是您尝试运行的文件)在命令提示符或终端中打开您的程序


它将显示文件中IDE有时没有显示的任何错误。

您可能正在IDE中使用特定的python安装,并使用另一个(system python?)来运行代码。您能否提供有关IDE的更多详细信息,以及在系统终端或IDE终端上执行的
哪个python
命令的结果是什么?您可能正在运行python 2.x,在这种情况下,模块名为
Tkinter
。我正在运行python 3x,使用IDLE,我尝试使用import-tkinter和from-tkinter-import*作为命令,这两个命令都在空闲状态下工作,但在通过启动程序运行时不起作用。我试着用最简单的软件让我的代码工作(tkinter和IDLE),但我真的很沮丧。我先编写了一个简单的hello world first
pip install tkinter
…收集tkinter无法找到满足tkinter要求的版本(来自版本:)找不到tkinter的匹配发行版
cd /Users/maclaren/Documents
python hellotkinter.py
cd /Users/maclaren/Documents
python3 hellotkinter.py
cd /Users/maclaren/Documents
py hellotkinter.py