本文介绍了未定义"Tk"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手.我下载了Spyder 2.3.1,并在Mac上运行Python 2.7.我尝试了这个示例程序:

I am new to Python. I downloaded Spyder 2.3.1 and am running Python 2.7 on my Mac. I tried this sample program:

from Tkinter import *
root = Tk()

w = Label(root, text="Hello, world!")
w.pack()

root.mainloop()

我跑步时收到错误消息:

When I run, I get the error message:

NameError: name 'Tk' is not defined

如果我查看文件Tkinter.py,它具有以下代码行:

If I look in the file Tkinter.py, it has the following lines of code:

from Tkinter import *
root = Tk()

w = Label(root, text="Hello, world!")
w.pack()

root.mainloop()

看起来像一个无限循环,但它抱怨的是"Tk"说名称未定义".任何帮助将不胜感激.

Looks like an infinite loop, but what it is complaining about is "Tk" saying "name not defined". Any help would be greatly appreciated.

p.s.我在终端"窗口中尝试了python -m idlelib.idle并收到错误NameError: name 'Tk' is not defined

p.s. I tried python -m idlelib.idle in a Terminal window and got the error NameError: name 'Tk' is not defined

推荐答案

文件名Tkinter.py阻止导入标准库模块Tkinter.

The filename Tkinter.py prevent the import of the standard library module Tkinter.

使用其他名称重命名文件.另外,您应该删除Tkinter.pyc(如果有的话).

Rename the file with different name. Also you shouold remove Tkinter.pyc if there it is.

这篇关于未定义"Tk"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 15:58