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

前端 未结 4 1849
孤独总比滥情好
孤独总比滥情好 2020-12-19 09:19

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 i         


        
4条回答
  •  北荒
    北荒 (楼主)
    2020-12-19 09:43

    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('')
    

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

    sudoku.kk(janela, '')
    

    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.

提交回复
热议问题