[Tkinter-discuss] lambada difficult to understand

Firat Ozgul ozgulfirat at gmail.com
Mon Aug 22 08:09:57 CEST 2011


With lambda your codes will look like this:

[code]
from Tkinter import *
fields = 'Name', 'Job', 'Pay'

def fetch(entries):
   for entry in entries:
       print 'Input => "%s"' % entry.get()       # get text

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 x: fetch(ents))
   Button(root, text='Fetch', command= lambda: fetch(ents)).pack(side=LEFT)
   root.mainloop()
[/code]

However, lambdas sometimes may be hard to read and hard to code.
Therefore I recommend you to use functools module instead. With
functools your code should look like this:

[code]
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

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()
[/code]

2011/8/22 守株待兔 <1248283536 at qq.com>:
> here is a example 9-18.pp3e\gui\tour\entry2.py  from  the book "programming
> python(third edition)",
> i revise it as following,change the  "fetch(entries)" to
> "fetch(event,entries)" ,and add ,event.widget
> to understand lambda is my goal,
> there is a question i can't solve,when i click button ,wrong output :
> TypeError: <lambda>() takes exactly 1 argument (0 given)
> would you kind to tell me how to revise it?
> command= (lambda event: fetch(event,ents))   #it is wrong ,but i don't know
> how to revise it?
>
>
> from Tkinter import *
> from quitter import Quitter
> fields = 'Name', 'Job', 'Pay'
>
> def fetch(event,entries):
>     for entry in entries:
>         print 'Input => "%s"' % entry.get() ,event.widget        # get text
>
> 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,ents)))
>     Button(root, text='Fetch',
>                  command= (lambda event: fetch(event,ents))).pack(side=LEFT)
>     Quitter(root).pack(side=RIGHT)
>     root.mainloop()
>
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>
>


More information about the Tkinter-discuss mailing list