Tkinter problem

Chad Netzer cnetzer at sonic.net
Sun Jun 22 19:34:26 EDT 2003


On Sun, 2003-06-22 at 07:02, Eirik wrote:

> What does the "if __name__=='__main__':" mean? I do not fully understand
> this example.

For the example I gave, you can ignore it (and remove the indenting
below).  But it is used in cases where you want to be able to import a
script (to get at its function definitions), as well as run it as a
script.

Basically, when you run a script, the __name__ global is equal to
'__main__', so the example doesn't execute those lines if you import the
script.  It is a common idiom.

Here is the example just as a straightforward script.  Let me know if it
makes the issue you asked about (setting up the proper callback) more
clear.



import sys
from Tkinter import *

def my_test_callback():
    global root
    root.t.set("Test-Text")

root = Tk()
root.t = StringVar()

root.l1 = Label(root, textvariable = root.t)
root.b1 = Button(root, text = "Button", command = my_test_callback)
root.bE = Button(root, text = "End", command = sys.exit)

root.l1.grid()
root.b1.grid()
root.bE.grid()

root.mainloop()


-- 
Chad Netzer <cnetzer at sonic.net>






More information about the Python-list mailing list