Tebe
Tebe

Reputation: 3214

python transfer parameter to function

I have one class that looks so:

class RSA:
 def encode(self,gui): 
    print "decoding started"

I want to call encode function from another class, when button, which is located in another class, is pressed. I can do it fine, if encode function has only one parameter - self.
I do it so:

Class GUI:
 self.parameter=8 # parameter which I want to pass
 def method(self):
  encode = Button(frame,command=rsa.encode)

And it works fine (if encode function has only one default parameter -self).

But I need to pass yet one parameter to the function rsa.encode. If I try to pass it so :

self.encode = Button(frame,command=rsa.encode (self.parameter) )
- It will be called once and immediately, when interpreter reaches this line and never again.

But it's not what I want, I want it to be called only when button is pressed. It could be easily done in C++, but here, I wonder that it's not work as there.

Thanks in advance for any replies!

Upvotes: 1

Views: 310

Answers (1)

mgilson
mgilson

Reputation: 310097

You want an anonymous function to create a closure:

self.encode = Button(frame,command=lambda : rsa.encode (self.parameter) )

Upvotes: 3

Related Questions