Discussion:
[Tkinter-discuss] A Button command callback question...
GKalman
2011-07-18 03:34:22 UTC
Permalink
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?
--
View this message in context: http://old.nabble.com/A-Button-command-callback-question...-tp32080551p32080551.html
Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20110717/9d99df59/attachment.html>
GKalman
2011-07-18 03:34:58 UTC
Permalink
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?
--
View this message in context: http://old.nabble.com/A-Button-command-callback-question...-tp32080554p32080554.html
Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.
johnmc
2011-07-18 11:09:20 UTC
Permalink
Post by GKalman
The following code fragment works OK. Question: why?
#======================================
from Tkinter import *
root=Tk()
lbl=Label(root,text='1').pack()
lbl.config(bg="yellow")
btn=Button(root,text="do", bg="cyan",command=doIt).pack()
root.mainloop()
#=======================================
The code fragment you posted does not work OK, assuming that if by OK you intend the
label background to change yellow upon invocation of the "do" button.

The reason why it does not work is you have bound lbl to the return of the pack
method, which is None. Obviously, you cannot configure the background colour of a
None object.

What I'm sure you intended was:

#=========================================
from Tkinter import *
root = Tk()
lbl = Label(root, text='1')
lbl.pack()

def doIt():
lbl.config(bg='yellow')

btn = Button(root, text='do', bg='cyan', command = doIt)
btn.pack()

root.mainloop()
#==========================================

Now, upon pressing the "do" button, the label background changes to yellow.
Post by GKalman
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?
The reason why has to do with scope. You have defined lbl in the global scope. Thus
the name "lbl" is available (defined) to the local scope of the function doIt. This
may have been purely accidentally fortuitous on your part but it is expected behaviour.

Regards,

John
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Michael Lange
2011-07-18 11:53:42 UTC
Permalink
Hi,

Thus spoketh GKalman <kalman_g at msn.com>
Post by GKalman
The following code fragment works OK. Question: why?
#======================================
from Tkinter import *
root=Tk()
lbl=Label(root,text='1').pack()
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.
Loading...