[Tkinter-discuss] [Fwd: Re: lambda function to simplify]

craf prog at vtr.net
Fri Dec 17 23:15:39 CET 2010


--------- Mensaje reenviado --------
> De: Michael Lange <klappnase at web.de>
> Para: tkinter-discuss at python.org
> Asunto: Re: [Tkinter-discuss] lambda function to simplify
> Fecha: Fri, 17 Dec 2010 22:30:44 +0100
> 
> Hi,
> 
> Thus spoketh craf <prog at vtr.net> 
> unto us on Fri, 17 Dec 2010 15:16:22 -0300:
> 
> (...)
> > I have access to the function (greeting) from the button and the window
> > close button.
> > 
> > It works fine, but wanted to know whether the use of lambda (lambda),
> > can be expressed in another way more elegant and less confusing.
> > 
> 
> If I understand you correctly, your problem is that the event callback
> receives the event instance as argument but the wm_protocol() doesn't?
> You can circumvent this by defining a default value for the event
> parameter, as in:
> 
> class App:
>     def __init__(self, master):
>         self.root = master
>         self.b1 = Tkinter.Button(master)
>         self.b1.pack()
>         self.b1.bind('<Button-1>', self.foo)
>         self.root.protocol('WM_DELETE_WINDOW', self.foo)
> 
>     def foo(self, event=None):
>         greeting(self)
> 
> In most cases it is probably even better (and less confusing) to avoid
> calling externally defined functions whereever possible and define prefer
> class methods instead, as in:
> 
> class App:
>     def __init__(self, master):
>         self.root = master
>         self.b1 = Tkinter.Button(master)
>         self.b1.pack()
>         self.b1.bind('<Button-1>', self.foo)
>         self.root.protocol('WM_DELETE_WINDOW', self.foo)
> 
>     def foo(self, event=None):
>         self.root.destroy()
> 
> BTW, I am not sure if you are aware of this, root.destroy() is already
> defined as default callback for WM_DELETE_WINDOW in Tkinter.py, so it's
> kind of redundant here; although it won't make much difference in most
> situations, some people say the canonical way ending a Tkinter app is to
> end the mainloop with root.quit() instead and destroy() the window
> after the mainloop ended, as in:
> 
> root = Tk()
> root.protocol('WM_DELETE_WINDOW', root.quit)
> (...)# a lot of code here
> root.mainloop()
> root.destroy()
> 
> That's what they do in IDLE, for example.
> 
> Regards
> 
> Michael
> 
> 
> 
> .-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.
> 
> Without facts, the decision cannot be made logically.  You must rely on
> your human intuition.
> 		-- Spock, "Assignment: Earth", stardate unknown
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss

Hi Michael.

Sorry, I do not explain well what I wanted ;=)

My guess is that if the instruction...

self.root.protocol('WM_DELETE_WINDOW', lambda:((lambda
e,widget=self:greeting(widget))(self.root)))

can be shortened to occupy the same line?

It's for a better understanding of the code.

Regards

Cristian Abarzúa




More information about the Tkinter-discuss mailing list