为什么按钮在tkinter中不起作用,当我单击它时,它显示了一个错误

发布于 2025-02-05 05:17:48 字数 368 浏览 3 评论 0原文

我正在尝试此代码。我想将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由如风 2025-02-12 05:17:49

问题的根源是一个通过bind自动获取事件参数的函数,但是命令选项调用的函数没有。诀窍是制作一个可以在有或没有参数的情况下调用的函数,并且不使用参数(因为不能保证它在那里)。

这样做的正常方法是制作event参数选项,这是这样做的:

def don(event=None):
    print("hello")

这将导致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 the command 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:

def don(event=None):
    print("hello")

This will cause event to be set to None if it's not passed in, which is harmless since the function doesn't use the event parameter.

半岛未凉 2025-02-12 05:17:49

您需要做的就是摆脱事件,或者正如我在我上方所说的事件=无。取决于您是否稍后要建立并添加一些参数

from tkinter import *
root = Tk()
root.geometry("600x500")

def don():
    print("hello")


root.bind("<Return>", don)
btn1 = Button(root, text="check! ", command=don).pack()

root.mainloop()

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

from tkinter import *
root = Tk()
root.geometry("600x500")

def don():
    print("hello")


root.bind("<Return>", don)
btn1 = Button(root, text="check! ", command=don).pack()

root.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文