2

我试图在 Python 3.3.1 中使用 Tkinter 开始我的新项目。所以我安装了 ActiveTcl 8.5.13,因为这似乎是必需的。

现在我运行这个简单的测试脚本;

import sys
from tkinter import *

mGui = Tk() # Instantiate the Tkinter object

但是,Tk()找不到实例;

iMac2011:Desktop allendar$ python3 tkinter.py
Traceback (most recent call last):
  File "tkinter.py", line 4, in <module>
    from tkinter import *
  File "/Users/allendar/Desktop/tkinter.py", line 8, in <module>
    mGui = Tk() # Instantiate the Tkinter object
NameError: name 'Tk' is not defined

我似乎无法在互联网上找到有关此问题的信息。它在我的 Ubuntu 发行版上没有任何问题。这是 OS X 需要某种特定方式调用库的某种问题吗?

4

1 回答 1

5

Your problem is the name of the file is tkinter.py So when import tkinter it imports the file itself instead of the actual tkinter module. I ran into this problem myself once, it took me a while to figure it out. Change the name of the file and it should fix your problem.

于 2013-06-18T13:12:12.887 回答