from Tkinter import *
fields = 'Name', 'Job', 'Pay'

def fetch(event,entries):
   for entry in entries:
       print 'Input => "%s"' % entry.get()       # get text
       print  event.widget 
      
      
def makeform(root, fields):
   entries = []
   for field in fields:
       row = Frame(root)                           # make a new row
       lab = Label(row, width=5, text=field)       # add label, entry
       ent = Entry(row)
       row.pack(side=TOP, fill=X)                  # pack row on top
       lab.pack(side=LEFT)
       ent.pack(side=RIGHT, expand=YES, fill=X)    # grow horizontal
       entries.append(ent)
   return entries

if __name__ == '__main__':
   root = Tk()
   ents = makeform(root, fields)
   root.bind('<Return>', lambda event,entries=ents: fetch(event,entries))       
   Button(root, text='Fetch', command= lambda event:fetch(event,entries)).pack(side=LEFT)  # #how to revise it?
   root.mainloop()

in the code ,the bind method is ok, how to revise "command=lambda event:fetch(event,entries)",to make it work too??

You never need to use lambda. Here, use functools.partial

from Tkinter import *
from functools import partial
fields = 'Name', 'Job', 'Pay'

def fetch(entries, event = None):
   for entry in entries:
       print 'Input => "%s"' % entry.get()       # get text
       if event:
           print  event.widget 
      
def makeform(root, fields):
   entries = []
   for field in fields:
       row = Frame(root)                           # make a new row
       lab = Label(row, width=5, text=field)       # add label, entry
       ent = Entry(row)
       row.pack(side=TOP, fill=X)                  # pack row on top
       lab.pack(side=LEFT)
       ent.pack(side=RIGHT, expand=YES, fill=X)    # grow horizontal
       entries.append(ent)
   return entries

if __name__ == '__main__':
   root = Tk()
   ents = makeform(root, fields)
   root.bind('<Return>', partial(fetch, ents))       
   Button(root, text='Fetch', command=partial(fetch, ents)).pack(side=LEFT)
   root.mainloop()

Edit: For this forum, it's better to configure your editor to indent python code with 4 spaces.

Edited 5 Years Ago by Gribouillis: n/a

To answer the original question about how to use lambda I give the following examples.

Without lambda:

def l(blah):
    print blah*5
l("f")

Regular lambda:

l = lambda blah: blah*5
print l("f")

To pass things to a lambda function when you define it:

print (lambda x: x*5)("f")

As you can see, regular functions can be used instead. Lambda is used as for more of functional programming aspect of python, not so much for OOP.

There is a good discussion here about the use of lambda.

This article has been dead for over six months. Start a new discussion instead.