[PyKDE] So how do I pass an argument to a function tied to a callback?

Aaron J. Ginn aaron.ginn at motorola.com
Fri Mar 2 20:13:11 GMT 2001


Well, since I'm not getting very far with the lambda issue, I'll pose my
question another way.  How do I pass an argument to a function that is
tied to a callback?  There are no examples of this in the PyQt
documentation that I can see.  The only way I know how to do this in
Tkinter is to use a lambda.  There must be another way to do this if
lambda is not an option.

The problem is that when python sees the parentheses on a callback
function, it immediately tries to execute the function; thus, a child
widget can potentially be created before the parent that calls it. 
There is an example in the Lambda.py testcase that I submitted
yesterday.  For instance, in the following connect statement, there are
no parentheses on the callback function (self.someFunction) because
there are no arguments to passed to it:

self.connect( widget, SIGNAL( "clicked()" ), self.someFunction )

However, in the following connect statement, an argument is passed to
self.someFunction; thus, parentheses are needed to pass num:

self.connect( widget, SIGNAL( "clicked()" ), self.someFunction( num ) )

As soon as python encounters that callback, it will attempt to execute
self.someFunction.  It won't wait for a button click.  Now to get around
this in Tkinter, a lambda is used such as the following:

a_button = Button(parent, command = lambda s=self,n=num:
			    s.someFunction(n) )

instead of

a_button = Button(parent, command = self.someFunction( num ) )

My first inclination in getting around this problem in PyQt was to use
the lambda as above, but that resulted in nothing but segfaults.

So how does one get around this problem?

Thanks,
Aaron

-- 
Aaron J. Ginn                    Phone: 480-814-4463
Motorola SemiCustom Solutions    Pager: 877-586-2318
1300 N. Alma School Rd.          Fax  : 480-814-4463
Chandler, AZ 85226 M/D CH260     mailto:aaron.ginn at motorola.com




More information about the PyQt mailing list