event :use command to replace bind

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

event :use command to replace bind

luofeiyu
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()

_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|

Re: event :use command to replace bind

Michael Lange
Hi,

Thus spoketh "守株待兔" <[hidden email]>
unto us on Thu, 25 Aug 2011 11:34:04 +0800:

> 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))

If you want to make your code simpler, I suggest a different approach.
Your problem appears to be the way arguments are passed to the fetch()
callback. It would be much easier to handle if you did not have to pass
the list of Entry widgets to the callback, then your fetch() function
could look like:

    def fetch(event=None):
        (...code...)

With this trick you can use the same callback as a Button command and as
a callback with bind() without any additional headaches.
The entry list could be stored globally or (better) you create a class
based on a Tkinter.Frame that contains all the Entries and make the fetch
() callback a class  method.

A simple example on how to create a custom subclass of Frame can be found
here:

http://www.java2s.com/Code/Python/GUI-Tk/SubclassFrameanduseit.htm

(and if you want to save two lines, omit this example's make_widgets()
method and just put the stuff from make_widgets() into __init__() ;)

Regards

Michael



.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

Madness has no purpose.  Or reason.  But it may have a goal.
                -- Spock, "The Alternative Factor", stardate 3088.7
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss