newbie with major "lambda" problem (perhaps a scope problem as well)
Joe Potter
jm7potter at hotmail.com
Tue Jun 26 11:34:26 EDT 2001
Hello all,
The following code comes from "Programing Python 2nd Edition" by Lutz.
My problem is with the Button function calls
in the " if __name__ == '__main__': " part of the code.
If one uses the lambda call as does Lutz,
then everything works like a champ.
However, a direct call to the function does not seem to do anything. No error
messages --- just no action.
This leads me to believe that I am missing some "VERY BIG DEAL" (TM) in Python.
Can someone tell this fool what he is missing?
Thanks in advance, Joe Potter
##### here is code in question
############################
# use StringVar variables and layout by columns
from Tkinter import *
from quitter import Quitter
fields = 'Name', 'Job', 'Pay'
info = []
def fetch(variables):
info = []
for variable in variables:
#print 'Input => "%s"' % variable.get() # get from var
info.append(variable.get())
for c in range(len(info)):
print info[c]
def makeform(root, fields):
form = Frame(root) # make outer frame
left = Frame(form) # make two columns
rite = Frame(form)
form.pack(fill=X)
left.pack(side=LEFT)
rite.pack(side=RIGHT, expand=YES, fill=X) # grow horizontal
variables = []
for field in fields:
lab = Label(left, width=5, text=field) # add to columns
ent = Entry(rite)
lab.pack(side=TOP)
ent.pack(side=TOP, fill=X) # grow horizontal
var = StringVar()
ent.config(textvariable=var) # link field to var
var.set('enter here')
variables.append(var)
return variables
if __name__ == '__main__':
root = Tk()
vars = makeform(root, fields)
# the "button" below works like a champ !!
#Button(root, text='Fetch',
#command=(lambda v=vars: fetch(v))).pack(side=LEFT)
# the "button" below does not do anything ??????
Button(root, text='Fetch', command=(fetch(vars))).pack(side=LEFT)
Quitter(root).pack(side=RIGHT)
root.bind('<Return>', (lambda event, v=vars: fetch(v)))
root.mainloop()
############################################
####### end code
More information about the Python-list
mailing list