为什么按钮在tkinter中不起作用,当我单击它时,它显示了一个错误
我正在尝试此代码。我想将don函数用于bind()
和命令
。它显示Don()缺少1所需的位置参数:“事件”。如何修复
我的代码
from tkinter import *
root = Tk()
root.geometry("600x500")
def don(Event):
print("hello")
root.bind("<Return>", don)
btn1 = Button(root, text="check! ", command=don).pack()
root.mainloop()
I am trying this code. I want to use don function for bind()
and command
. It is showing don() missing 1 required positional argument: 'Event'. how to fix it
my code
from tkinter import *
root = Tk()
root.geometry("600x500")
def don(Event):
print("hello")
root.bind("<Return>", don)
btn1 = Button(root, text="check! ", command=don).pack()
root.mainloop()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题的根源是一个通过
bind
自动获取事件参数的函数,但是命令
选项调用的函数没有。诀窍是制作一个可以在有或没有参数的情况下调用的函数,并且不使用参数(因为不能保证它在那里)。这样做的正常方法是制作
event
参数选项,这是这样做的:这将导致
event
将其设置为none
如果未传递,则无害,因为该函数不使用event
参数。The root of the problem is that a function called via
bind
automatically gets an event parameter, but a function called by thecommand
option does not. The trick is to make a function that can be called with or without a parameter, and which doesn't use the parameter (since it isn't guaranteed to be there).The normal way to do this is to make the
event
parameter options, which is done like this:This will cause
event
to be set toNone
if it's not passed in, which is harmless since the function doesn't use theevent
parameter.您需要做的就是摆脱事件,或者正如我在我上方所说的事件=无。取决于您是否稍后要建立并添加一些参数
All you need to do is get rid of Event or as the person said above me Event=None. Depends if you are going to build on it later and add some parameters