Tkinter and classes

Frank Niessink frankn=news at cs.vu.nl
Mon Aug 23 03:37:56 EDT 1999


Justin Muir <jkmuir at uniserve.com> wrote:

> class Helloworld:
>     def __init__(self, master):
>             _lblstr = StringVar()
>             _lblstr.set("hello world")

>             frm = Frame(master)
>             frm.pack()

>             but = Button(frm, text='Hello!', command=setLabel)
>             but.grid(row=0)

>             lbl = Label(frm, textvariable=_lblstr)
>             lbl.grid(row=1)

>     def setLabel(self):
>             _lblstr.set("HELLO WORLD!!")

> This will run, but barfs out on the callback.
> Am I misunderstanding the relationship between the callback to the
> namespace of the class?

Try replacing _lblstr by self._lblstr to make it an instance variable.

As you programmed it above _lblstr is just local to the __init__ function
and disappears as soon as the function is finished. So setLabel doesn't 
know anything about a _lblstr.

Cheers, Frank

-- 
"I think we have different value systems."  "Well mine's better."  "That's
according to your ... oh never mind."
	-- Douglas Adams, 'Mostly Harmless'




More information about the Python-list mailing list