如何将命令绑定到Tkinter中的enter键?

2024-10-05 14:28:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我见过像bind命令这样的解决方案,但当我给它参数(“”,self.checkguess)时,它会出错,说需要一个参数

self.input = Entry(self.win)
self.input.place(x = 0, y = 80)
self.input.focus_set()
self.attempt = Button(self.win, text = "Guess", width = 6, height = 2, command = self.check_guess)
self.attempt.place(x = 0, y = 120)
self.win.bind('<Return>', self.check_guess)

win是窗口的名称

以下是错误消息:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
TypeError: check_guess() takes 1 positional argument but 2 were given

下面是check_guess的代码:

def check_guess(self):
        print("hello")
        global guesses
        global correctguesses
        global word2

Tags: in命令selfinput参数bindcheckplace
1条回答
网友
1楼 · 发布于 2024-10-05 14:28:43

将键绑定到函数激活时,您正在添加多个参数

当你定义“检查猜测”时

而不是def check_guess(self):

尝试:

def check_guess(*args):

*args几乎允许通过一个函数传递多个参数

相关问题 更多 >