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

I created a Sudoku game in python with Tk.

I got an error regarding the keypress function for the 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 do not mind where the error is. I included myself in my function

+4
source share
4 answers

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

Say your kk method is used in several places, and you don’t want to send any random variable to take the place of “another_parameter” shown below (disabling the Christian answer),

def kk(self, another_parameter):

, , . , "another_parameter", bind(), Lambda, :

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

, kk, lambda ( , , , ). , , - , kk ( , "e" , ). , kk

def kk(self):

( !), . , , , , , bind, bind.

+8

tkinter, ( , ),

bind(some_string, some_function)

function, string.

kk

def kk(self):

, . self.kk bind(), ,

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

! kk.

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

, janela sudoku. , !!!

?

, , , kk :

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

.. Python. , SomeClassName sudoku.

+5

kk :

def kk(self, event):
    lb[0][0]['text']='2'

kk ( ), kk , event, self.

+4

Change the definition kkto

def kk(self, event):
    ...

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

Now when it tkcalls func(event)what looks like self.kk(event), the number of arguments is wrong.

+4
source

Source: https://habr.com/ru/post/1541855/


All Articles