By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

strange partial function aciton

P: 18
code1 ok
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. from functools import partial
  3. fields = 'Name', 'Job', 'Pay'
  4.  
  5. def fetch(entries,event):
  6.     for entry in entries:
  7.        print 'Input => "%s"' % entry.get()        
  8.     print  event
  9.  
  10.  
  11. def makeform(root, fields):
  12.     entries = []
  13.     for field in fields:
  14.         row = Frame(root)                          
  15.         lab = Label(row, width=5, text=field)      
  16.         ent = Entry(row)
  17.         row.pack(side=TOP, fill=X)                  
  18.         lab.pack(side=LEFT)
  19.         ent.pack(side=RIGHT, expand=YES, fill=X)    
  20.         entries.append(ent)
  21.     return entries
  22.  
  23.  
  24. if __name__ == '__main__':
  25.  
  26.     root = Tk()
  27.     ents = makeform(root, fields)
  28.     root.bind('<Return>', partial(fetch,ents))
  29.     root.mainloop() 
  30.  
code2 fail i don't understand why ?
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. from functools import partial
  3. fields = 'Name', 'Job', 'Pay'
  4.  
  5. def fetch(entries,event):
  6.     for entry in entries:
  7.        print 'Input => "%s"' % entry.get()        
  8.     print  event
  9.  
  10.  
  11. def makeform(root, fields):
  12.     entries = []
  13.     for field in fields:
  14.         row = Frame(root)                          
  15.         lab = Label(row, width=5, text=field)      
  16.         ent = Entry(row)
  17.         row.pack(side=TOP, fill=X)                  
  18.         lab.pack(side=LEFT)
  19.         ent.pack(side=RIGHT, expand=YES, fill=X)    
  20.         entries.append(ent)
  21.     return entries
  22.  
  23.  
  24. if __name__ == '__main__':
  25.  
  26.     root = Tk()
  27.     ents = makeform(root, fields)
  28.     root.bind('<Return>', partial(fetch,entries=ents))
  29.     root.mainloop() 
  30.  
code3: fail i know the reason
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. from functools import partial
  3. fields = 'Name', 'Job', 'Pay'
  4.  
  5. def fetch(event,entries):
  6.     for entry in entries:
  7.        print 'Input => "%s"' % entry.get()        
  8.     print  event
  9.  
  10.  
  11. def makeform(root, fields):
  12.     entries = []
  13.     for field in fields:
  14.         row = Frame(root)                          
  15.         lab = Label(row, width=5, text=field)      
  16.         ent = Entry(row)
  17.         row.pack(side=TOP, fill=X)                  
  18.         lab.pack(side=LEFT)
  19.         ent.pack(side=RIGHT, expand=YES, fill=X)    
  20.         entries.append(ent)
  21.     return entries
  22.  
  23.  
  24. if __name__ == '__main__':
  25.  
  26.     root = Tk()
  27.     ents = makeform(root, fields)
  28.     root.bind('<Return>', partial(fetch,ents))
  29.     root.mainloop() 
  30.  
code4: ok
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. from functools import partial
  3. fields = 'Name', 'Job', 'Pay'
  4.  
  5. def fetch(event,entries):
  6.     for entry in entries:
  7.        print 'Input => "%s"' % entry.get()        
  8.     print  event
  9.  
  10.  
  11. def makeform(root, fields):
  12.     entries = []
  13.     for field in fields:
  14.         row = Frame(root)                          
  15.         lab = Label(row, width=5, text=field)      
  16.         ent = Entry(row)
  17.         row.pack(side=TOP, fill=X)                  
  18.         lab.pack(side=LEFT)
  19.         ent.pack(side=RIGHT, expand=YES, fill=X)    
  20.         entries.append(ent)
  21.     return entries
  22.  
  23.  
  24. if __name__ == '__main__':
  25.  
  26.     root = Tk()
  27.     ents = makeform(root, fields)
  28.     root.bind('<Return>', partial(fetch,entries=ents))
  29.     root.mainloop() 
  30.  

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.
Aug 26 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.