Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python tkinter binding a function with multiple parameters [duplicate]

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 ..

like image 224
user2103768 Avatar asked Sep 26 '26 15:09

user2103768


1 Answers

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.

like image 74
mgilson Avatar answered Sep 28 '26 04:09

mgilson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!