Python: function takes 1 positional argument but 2 were given, how? Python: function takes 1 positional argument but 2 were given, how? tkinter tkinter

Python: function takes 1 positional argument but 2 were given, how?


I see that this has been answered, but I have a way I really prefer and that you and others may appreciate.

Say that your method kk is used in multiple spots, and you don't want to have to send in some random variable to take up the spot of "another_parameter" shown below (working off of Christian's response),

def kk(self, another_parameter):

Personally, I think parameter lists should have ONLY what they need. So, as long as you have no need for the "another_parameter" variable that the bind() function sends, I suggest using Lambda by doing the following:

lb[0][0].bind('<KeyPress-2>',lambda e:self.kk())

I think you need the two parentheses after kk now because lambda is actually going to run that function with it's parameters (in your case, if you remove the one I said to, there would be none). What lambda is doing for us is catching the parameter being thrown to kk from the bind function (that is what the 'e' is after lambda, it represents the argument). Now, we don't need it in our parameter list, and we can resume our kk definition to be

def kk(self):

I started using the approach by Christian (which works!) but I didn't like the extra variable. Obviously both methods work, but I think this one helps, especially if the function being called in bind is used more than once and not necessarily used by a bind call.


I'm not a tkinter expert, but it seems (by what I've read so far) that the method

bind(some_string, some_function)

calls function passing the parameter string to it.

You have declared the method kk like

def kk(self):

and it means that it is only expecting one argument. You are also passing the method self.kk to bind(), which means that it will be called like

self.kk('<KeyPress-2>')

There is the problem! That call, in fact, is passing two arguments to the method kk. It's equivalent to

sudoku.kk(janela, '<KeyPress-2>')

Note that janela is the actual instance of the class sudoku. Coming back to the problem, you are passing two arguments!!!

How can you solve it?

As I said I'm not an expert on this topic, but my guess is to declare the method kk with two parameters:

def kk(self, another_parameter):    # ...

Note: I would recommend you to follow Python naming conventions. In other words, class names should be like SomeClassName or Sudoku.


Change kk definition to

def kk(self, event):    ...

then when you pass self.kk as callback, tk will call it like func(event) (self.kk(event)) and everything will be fine.

Now when tk calls func(event), which is like self.kk(event), the number of arguments is wrong.