[Tkinter-discuss] event :use command to replace bind
Michael Lange
klappnase at web.de
Thu Aug 25 10:52:04 CEST 2011
Hi,
Thus spoketh "守株待兔" <1248283536 at qq.com>
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
More information about the Tkinter-discuss
mailing list