On 09/12/11 19:03, Richard Lyons wrote:
I have tried to enter the first sample program from p. 19 in Grayson:
Python and Tkinter Programming. When I run the program I get an error
as follows:

Traceback (most recent call last):
File "/home/dick/Desktop/calc1.py", line 50, in <module>
if _name_ == '_main_':
NameError: name '_name_' is not defined

The error makes sense because nowhere in the code is _main_ defined.

Read the error again. It is actually _name_ that it is complaining about! However, neither _name_ or "_main_" (notoice the quotes!) are defined.

The real problem is that you need a double underscore on each side. This is a special convention that is used in Python to indicate special "magic" being performed by Python behind the scenes.

In this case the magic variable __name__ is assigned the name of the file if the file is used as a module but the name "__main__" if the file is run as a program. (In python there is no real difference between programs and module files. One file can do both jobs)

You will come across several other magic names as you explore python, most notably __init__ when looking at classes.

One thing to beware. Grayson's book is based on a very old version of Python so some things may have changed a little. If you hit unexplainable errors post them here and we can probably help you over them. Also Grayson's book takes no prisoners in its code. There is a lot of it and he assumes you know Python and GUI programming in general quite well. You may want to go through the standard Python tutorials before plunging too far into Tkinter!

HTH,


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to