How to access object created in Main?

Rhodri James rhodri at wildebst.demon.co.uk
Sun Mar 29 18:03:48 EDT 2009


On Sun, 29 Mar 2009 19:12:23 +0100, Muddy Coder <cosmo_general at yahoo.com>  
wrote:

> Hi Folks,
>
> I need to update the text field of a Label created in Main, but can't
> find a way to do it. Please take a look at my code:
>
> from Tkinter import *
>
> def makemenu(r)
>    amenu = Menu(r)
>    amenu.add_command(....., command=update_label)
>
> def update_label():
>     how to access mesg created in __main__ ?
>
> if __name__ == '__main__':
> root = Tk()
> mesg = Label(root, text='foo\nbar\nfoo')
> mesg.pack(expand=YES, fill=BOTH)
> root.mainloop()

A style point: the "if __name__ == '__main__'" construct implies that
you expect your file to be included as a module at some time in the
future.  When that time comes, having your menu structure rely on
module globals that will not exist (because the "mesg = Label()"
doesn't get run) will cause your program to explode messily in a
deeply confusing way.


-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list