Python: function takes 1 positional argument but 2 were given, how?
Question: I was creating a Sudoku Game in python with Tk.

I got a error about the function on a keypress for a button

    
    
    from random import randint
    from tkinter import *
    
    class sudoku:
        global root,result,lb
        def __init__(self):
            self.aleatoriedade()
            for k in range(9):
                j=randint(0,80)
                x=j//9
                y=j-(x*9)
                lb[x][y]['text']=result[x][y]
            lb[0][0].bind('<KeyPress-2>',self.kk)
            #setted this for test
            root.mainloop()
    
        def kk(self):
            lb[0][0]['text']='2'
    
    
        def aleatoriedade(self):
            for i in range(9):
                var=0
                while var in result[0]:
                    var=randint(1,9)
                result[0][i]=var
    
            for i in range(1,9):
                for j in range(9):
                    result[i][j]=result[0][field[i][j]-1]
    
    #MAIN()
    n = 3
    field = [[(i*n + i//n + j) % (n*n) + 1 for j in range(9)]for i in range(9)] 
    result = [[None for i in range(9)]for i in range(9)]
    lb=[[None for i in range(9)]for i in range(9)]
    x=0
    y=0
    root=Tk()
    
    for i in range(9):
        for j in range(9):
            lb[i][j]=Button(root,font=("Verdana",'13',"bold"),bd=1,height=3,width=6)
            if (i in (0,1,2,6,7,8) and j in (0,1,2,6,7,8))or(i in (3,4,5) and j in (3,4,5)):
                lb[i][j]['bg']='white'
            lb[i][j].grid(row=i,column=j)
    janela=sudoku()
    

and this error/exception in `lb[0][0].bind('<KeyPress-2>',self.kk)`

    
    
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\Python33\lib\tkinter\__init__.py", line 1489, in __call__
        return self.func(*args)
    TypeError: kk() takes 1 positional argument but 2 were given
    

I don't mind where is the error. I have included the self on my function


Answer: 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](http://www.python.org/dev/peps/pep-0008/#naming-conventions). In
other words, class names should be like `SomeClassName` or `Sudoku`.

