3

I have a general question that I can't really find an answer to so hopefully you guys can help. I have a function that takes 3 parameters, below is an example of what I have.

def someFunction(self, event, string):

   do stuff ..

self.canvas.bind("<Button-1>", self.someFunction("Hello"))

When I run this, I get an error saying that I passed someFunction 2 arguments instead of 3. I'm not sure why ..

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
user2103768
  • 65
  • 2
  • 4

2 Answers2

13

Here you're binding the result of someFunction (or trying to anyway). This fails because when python tries to get the result of someFunction, it calls it only passing 1 argument ("Hello") when someFunction really expects 2 explicit arguments. You probably want something like:

self.canvas.bind('<Button-1>',lambda event: self.someFunction(event,"Hello"))

This binds a new function (which is created by lambda and wraps around self.someFunction) which passes the correct arguments.

mgilson
  • 300,191
  • 65
  • 633
  • 696
0

Or,

def someFunction(self, string):
   def fn(*arg)
      print string
   return fn


self.canvas.bind("<Button-1>",self.someFunction("Hello!"))
empax
  • 80
  • 2
  • 8