[Tkinter-discuss] strange partial function aciton

守株待兔 1248283536 at qq.com
Fri Aug 26 09:18:45 CEST 2011


code1  ok
from Tkinter import *
from functools import partial
fields = 'Name', 'Job', 'Pay'
    
def fetch(entries,event):
    for entry in entries:
       print 'Input => "%s"' % entry.get()        
    print  event
    
    
def makeform(root, fields):
    entries = []
    for field in fields:
        row = Frame(root)                          
        lab = Label(row, width=5, text=field)      
        ent = Entry(row)
        row.pack(side=TOP, fill=X)                  
        lab.pack(side=LEFT)
        ent.pack(side=RIGHT, expand=YES, fill=X)    
        entries.append(ent)
    return entries
          
    
if __name__ == '__main__':
          
    root = Tk()
    ents = makeform(root, fields)
    root.bind('<Return>', partial(fetch,ents))
    root.mainloop() 

code2   fail  i don't understand  why  ?
from Tkinter import *
from functools import partial
fields = 'Name', 'Job', 'Pay'
    
def fetch(entries,event):
    for entry in entries:
       print 'Input => "%s"' % entry.get()        
    print  event
    
    
def makeform(root, fields):
    entries = []
    for field in fields:
        row = Frame(root)                          
        lab = Label(row, width=5, text=field)      
        ent = Entry(row)
        row.pack(side=TOP, fill=X)                  
        lab.pack(side=LEFT)
        ent.pack(side=RIGHT, expand=YES, fill=X)    
        entries.append(ent)
    return entries
          
    
if __name__ == '__main__':
          
    root = Tk()
    ents = makeform(root, fields)
    root.bind('<Return>', partial(fetch,entries=ents))
    root.mainloop() 
    
code3:  fail  i know the reason
from Tkinter import *
from functools import partial
fields = 'Name', 'Job', 'Pay'
    
def fetch(event,entries):
    for entry in entries:
       print 'Input => "%s"' % entry.get()        
    print  event
    
    
def makeform(root, fields):
    entries = []
    for field in fields:
        row = Frame(root)                          
        lab = Label(row, width=5, text=field)      
        ent = Entry(row)
        row.pack(side=TOP, fill=X)                  
        lab.pack(side=LEFT)
        ent.pack(side=RIGHT, expand=YES, fill=X)    
        entries.append(ent)
    return entries
          
    
if __name__ == '__main__':
          
    root = Tk()
    ents = makeform(root, fields)
    root.bind('<Return>', partial(fetch,ents))
    root.mainloop() 

code4:  ok
from Tkinter import *
from functools import partial
fields = 'Name', 'Job', 'Pay'
    
def fetch(event,entries):
    for entry in entries:
       print 'Input => "%s"' % entry.get()        
    print  event
    
    
def makeform(root, fields):
    entries = []
    for field in fields:
        row = Frame(root)                          
        lab = Label(row, width=5, text=field)      
        ent = Entry(row)
        row.pack(side=TOP, fill=X)                  
        lab.pack(side=LEFT)
        ent.pack(side=RIGHT, expand=YES, fill=X)    
        entries.append(ent)
    return entries
          
    
if __name__ == '__main__':
          
    root = Tk()
    ents = makeform(root, fields)
    root.bind('<Return>', partial(fetch,entries=ents))
    root.mainloop() 
    
there are  four  codes,code1,code4 it's ok,i know why;
code2,code3,it's not ok,i understand why code2 can't work,
would you mind to tell me why  code3  can't work??
the output is:
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__
    return self.func(*args)
TypeError: fetch() got multiple values for keyword argument 'entries'

i don't understand it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20110826/d0a5e76e/attachment.html>


More information about the Tkinter-discuss mailing list