Python:函数采用1个位置参数,但给出了2个,如何? [英] Python: function takes 1 positional argument but 2 were given, how?

查看:184
本文介绍了Python:函数采用1个位置参数,但给出了2个,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Tk在python中创建数独游戏。

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

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

我不介意错误在哪里。我已经将self包含在我的函数中

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

推荐答案

我看到已经解决了这个问题,但是我有一种我真正喜欢的方法,并且您和其他人可能会感激。

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

说您的方法kk用于多个位置,并且您不想发送任何随机变量来占据显示的 another_parameter的位置下面(根据Christian的回应),

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):

就个人而言,我认为参数列表应该只包含它们所需的内容。因此,只要您不需要bind()函数发送的 another_parameter变量,我建议通过以下操作使用Lambda:

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

我认为您现在需要在kk之后加上两个括号,因为lambda实际上将使用其参数运行该函数(在您的情况下,如果删除我说过的那个,会是没有)。 lambda为我们所做的就是捕获从bind函数抛出的参数(即lambda之后的 e表示参数)。现在,我们不需要在参数列表中使用它,并且可以将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):

我开始使用Christian的方法(可行!),但我不喜欢额外的变量。显然,这两种方法都可以,但是我认为这很有用,特别是如果在bind中调用的函数被使用了多次且不一定由bind调用使用。

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.

这篇关于Python:函数采用1个位置参数,但给出了2个,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆