'Tk' is not defined [duplicate]

耗尽温柔 提交于 2020-01-06 08:14:06

问题


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

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()

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. I tried python -m idlelib.idle in a Terminal window and got the error NameError: name 'Tk' is not defined


回答1:


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

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



来源:https://stackoverflow.com/questions/26573776/tk-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!