【问题标题】:Takes exactly one argument, two given [closed]只接受一个参数,两个给定[关闭]
【发布时间】:2023-03-23 15:40:02
【问题描述】:
from Graphics import *

win = Window()
button = Button ((200,100), "click")
button.draw(win)

score=100

def user_click(score):
    score+=10
    print(score)

button.connect('',user_click)

我想让这个函数给分数加 10 分。但是我收到了这个错误

只接受一个参数,两个给定

我将如何修复这个错误来做到这一点?

【问题讨论】:

  • 完整的回溯会有所帮助,并准确指定您正在使用的库。
  • 我假设回调将两个变量传递给您的回调函数user_click,这两个变量都不是分数。您可能希望将回调设置为等于 lambda。 button.connect('', lambda x,y: user_click(score))
  • 这不是 Tkinter。这可能是 PyGTK...您使用的是什么库?
  • 我只是在使用图形库。我这样做是为了一个学校项目,而图形是我们唯一被允许使用的库。

标签: python button graphics


【解决方案1】:

Gui 元素回调通常需要一个名为“事件”的参数,这是您传递给 user_click 的第二个参数。

试试这个:

def user_click(event, score):
    score+=10
    print(score)

【讨论】:

  • 在我这样做之后,我收到另一个错误,提示“+=: 'Button' and 'int' 的操作数类型不受支持。”你知道我该如何解决这个问题吗?
  • 这听起来像是传入函数声明的参数顺序。尝试:def user_click(score, event) 代替
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 2021-12-04
  • 2017-03-27
  • 2013-07-08
  • 1970-01-01
  • 2019-06-24
相关资源
最近更新 更多