event :use command to replace bind

守株待兔 1248283536 at qq.com
Wed Aug 24 23:34:04 EDT 2011


here  is my code ,it can run ,i  want to make it simpler,
to  change  three  sentences  to one,
    xb =Button(root, text='Fetch')
    xb.pack(side=LEFT)
    xb.bind("<Button-1>", partial(fetch, entries=ents))
    
i  write:
    Button(root, text='Fetch',command=partial(fetch, entries=ents)).pack(side=LEFT)

that is to change code1  into  code2 ,when you click  the button"fetch",the output is :
TypeError: fetch() takes exactly 2 arguments (1 given)

how to revise it?

code1:(it's 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.widget 
     
     
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))     
    xb =Button(root, text='Fetch')
    xb.pack(side=LEFT)
    xb.bind("<Button-1>", partial(fetch, entries=ents))
    root.mainloop() 
           
code2:

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.widget 
     
     
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))
    Button(root, text='Fetch',command=partial(fetch, entries=ents)).pack(side=LEFT)    
    root.mainloop()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110825/66639ccc/attachment.html>


More information about the Python-list mailing list