A Button command callback question...

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

A Button command callback question...

GKalman-2
The following code fragment works OK. Question: why? #====================================== from Tkinter import * root=Tk() lbl=Label(root,text='1').pack() def doIt(): lbl.config(bg="yellow") btn=Button(root,text="do", bg="cyan",command=doIt).pack() root.mainloop() #======================================= My question is: how is the instance lbl.config(...) recognized in the doIt callback fn when called from the main w/o an argument referencing lbl?
Reply | Threaded
Open this post in threaded view
|

Re: A Button command callback question...

Michael Lange
Hi,

Thus spoketh GKalman <[hidden email]>
unto us on Sun, 17 Jul 2011 20:34:22 -0700 (PDT):

>
> The following code fragment works OK.  Question: why?
> #======================================
> from Tkinter import *
> root=Tk()
>
> lbl=Label(root,text='1').pack()
>
> def doIt():
>     lbl.config(bg="yellow")
>
> btn=Button(root,text="do", bg="cyan",command=doIt).pack()
>
> root.mainloop()
> #=======================================
> My question is: how is the instance lbl.config(...) recognized in the
> doIt callback fn when called from the main w/o an argument referencing
> lbl?

When I run this example and hit the button I get:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in __call__
    return self.func(*args)
  File "test6.py", line 8, in doIt
    lbl.config(bg="yellow")
AttributeError: 'NoneType' object has no attribute 'config'

as one would expect.

Regards

Michael


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

        "What happened to the crewman?"
        "The M-5 computer needed a new power source, the crewman merely
got in the way."
                -- Kirk and Dr. Richard Daystrom, "The Ultimate Computer",
                   stardate 4731.3.
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss