TKinter, buttonwidget response problem(1) and all btns the same size(2)!

Fredrik Lundh fredrik at pythonware.com
Sat Apr 5 09:34:19 EDT 2008


Francesco Bochicchio wrote:

> It should be added here that in Python you have several ways get around 
> this Tkinter limitation and pass an user argument to the callback. Once
> upon a time , back in Python 1.x, I used to do something like this:
> 
> class CallIt:
> 	def __init__(self, f, *args):
> 		self.f = f
> 		self.args = args
> 	def __call__(self):
> 		return apply(self.f, self.args)
> 
> and then, to do what the OP wanted to do:
> 	command = CallIt(self.Display, 1)
> 
> but nowadays, you can achieve the same effect with:
> 	command = functtools.partial(self.Display,1)

or, much clearer for non-guru programmers, and a lot easier to extend 
when you realize that you have to do more than just calling a single 
method, use a local callback function:

     def do_display():
         self.Display(1)

     w = Button(callback=do_display)

local functions are cheap in Python; creating a new one for each button 
is very inexpensive.

for very simple callbacks, you can use the lambda syntax as well:

     w = Button(callback=lambda: self.Display(1))

</F>




More information about the Python-list mailing list