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

Francesco Bochicchio bockman at virgilio.it
Sat Apr 5 09:22:19 EDT 2008


Il Fri, 04 Apr 2008 20:26:13 -0700, 7stud ha scritto:

> On Apr 4, 7:06 pm, skanem... at yahoo.se wrote:
>> 1st question:
>>
>> when i run this program 1 will be printed into the interpreter when i
>> run it BUT without me clicking the actual button. when i then click the
>> button "1", nothing happens.
>>
>> obv i dont want any output when i dont push the button but i want it
>> when i do.
>>
>> what am i doing wrong here?
>>

...

> 
> The same thing is happening in this portion of your code:
> 
> command = self.Display(1)
> 
> That code tells python to execute the Display function and assign the
> function's return value to the variable command.  As a result Display
> executes and 1 is displayed.  Then since Dispay does not have a return
> statement, None is returned, and None is assigned to command. Obviously,
> that is not what you want to do.
> 
> What you want to do is assign a "function reference" to command so that
> python can execute the function sometime later when you click on the
> button.  A function reference is just the function name without the '()'
> after it.  So you would write:
> 
> command = self.Display
> 
> But writing it like that doesn't allow *you* to pass any arguments to
> Display().  In addition, *tkinter* does not pass any arguments to
> Display when tkinter calls Display in response to a button click.  As a
> result, there is no way to pass an argument to Display.
> 

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)

Ciao
----
FB



More information about the Python-list mailing list