回复: lambada difficult to understand

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

回复: lambada difficult to understand

luofeiyu
dear Firat Ozgul:
 think for your help.
you misunderstand my meaning,i want to get the output:
print 'Input => "%s"' % entry.get()
print event.widget

please don't revise the function  fetch,

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 x: fetch(ents))          #how to revise it?
   Button(root, text='Fetch', command= lambda: fetch(ents)).pack(side=LEFT)  #
#how to revise it?
   root.mainloop()

how to fix:
   root.bind('<Return>', lambda x: fetch(ents))
   Button(root, text='Fetch', command= lambda: fetch(ents)).pack(side=LEFT) 

to make the code to run ,what i want to get are:

        print 'Input => "%s"' % entry.get()       # get text
        print  event.widget 

any help appreciated.
      
------------------ 原始邮件 ------------------
发送时间: 2011年8月22日(星期一) 下午2:09
收件人: "Tkinter-discuss"<[hidden email]>;
主题: Re: [Tkinter-discuss] lambada difficult to understand
 
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 守株待兔 <[hidden email]>:

> 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
> [hidden email]
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>
>
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss

_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss