How to get rid of NameError in Python?
Hi I just executed the following code :
from Tkinter import *
class LabelDemo( Frame ):
def __init__( self ):
Frame.__init__( self )
self.pack( expand = YES, fill = BOTH )
self.master.title( "Labels" )
self.Label3 = Label( self, bitmap = "warning" )
self.Label3.pack( side = LEFT )
if __name__ == "__main__":
LabelDemo().mainloop()
It gi开发者_开发知识库ves me NameError: name 'LabelDemo' is not defined.
What could be the reason?
That code worked fine for me also. There must be an indentation error somewhere. Is that the entirety of the file? Have you got tabs mixed with spaces anywhere?
Edit:
This is your traceback:
Traceback (most recent call last):
File "warning.py", line 3, in <module> class LabelDemo(Frame):
File "warning.py", line 14, in LabelDemo LabelDemo().mainloop()
NameError: name 'LabelDemo' is not defined
That is saying that the call to LabelDemo().mainloop()
is happening within the class definition of LabelDemo(Frame)
. That means that you have got the if __name__ == "__main__"
indented. Remove the indentation around the if __name__ ..
line, and it should work fine.
Final Edit:
There was an edit to the question that formatted the code, presumably because the OP didn't know how to do so. But in the edit, the indentation would have been repaired where it was previously broken.
精彩评论