How to access object created in Main?

M

Muddy Coder

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()

What I need to do is simple: when Menu (amenu) is clicked, it should
execute update_label function that is expected to update the text of
Label mesg in MAIN, with a syntax like: mesg.config(text='new text')
What I don't know is the path of accessing this object mesg. I tried
root.mesg, no good. I am confused here: since mesg is created on root
as its master, why root.mesg is not its path? Can somebody help me
out? Thanks a lot!

Muddy Coder
 
D

Dennis Lee Bieber

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()

What I need to do is simple: when Menu (amenu) is clicked, it should
execute update_label function that is expected to update the text of
Label mesg in MAIN, with a syntax like: mesg.config(text='new text')
What I don't know is the path of accessing this object mesg. I tried
root.mesg, no good. I am confused here: since mesg is created on root
as its master, why root.mesg is not its path? Can somebody help me
out? Thanks a lot!
You create an anonymous Label object which is connected to the root
object.

The name "mesg" is just a way to reference the anonymous object
returned by Label() -- "mesg" only exists in the scope of your program
module. Per Lundh's "Intro", the library assigns an internal name to the
objects for use by the low-level TCL/TK system.
18871320
root.winfo_children()
[ said:
root.winfo_children()[0].winfo_name()
'18871320'
root.children
{'18871320': said:
root.children["18871320"]
<Tkinter.Label instance at 0x011FF418>

Typically, you'd create your "application" as a class, and for
anything you need to reference within it, use an instance variable

class myApp(object):
def __init__(self):
self.root = Tk()
self.mesg = Label(self.root,...)
...

def update_label(self):
self.mesg.config(...)
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,092
Latest member
vinaykumarnevatia1

Latest Threads

Top