问题
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