每次使用Tkinter单击按钮时都会出现此错误。我不太确定0参数是什么意思,因为我认为回调给出了两个参数。回溯错误:
Exception in Tkinter callback Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in call return self.func(*args)
TypeError: callback() takes exactly 2 arguments (0 given)
以下是与回调相关的所有代码
def callback(input_set, user_set):
user_score = 0
if len(input_set & user_set) == 0:
user_score += len(input_set - user_set) * 2
for multiplier, user_set in enumerate(user_sets, 1):
user_score += len(input_set & user_set) * multiplier
print "Congratulations, you've scored " + str(user_score) + " points!"
# Creates button to calculate score
self.button = Tkinter.Button(self, font="Arial", text="Click to get your score",
width = 45, pady = 5, command = callback)
self.button.grid(column=0, row=3, sticky="", columnspan=2)
答案 0 :(得分:3)
在你的给定函数 def callback(input_set, user_set):
中,它需要两个参数,我认为当你使用Tkinter单击按钮时,这两个参数不会传递给回调函数。你必须检查哪些参数传递给函数。
答案 1 :(得分:2)
command = callback
在这里你必须给你传递给回调的args。使用command=lambda:callback(arg1,arg2)
其中arg1& arg2是提供被调用函数所需的参数