python函数参数位置_Python:函数采用1个位置参数,但给出了2个,如何?

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

解决方案

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('',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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值