Tkinter 中的按钮小部件在未单击时执行代码

问题描述 投票:0回答:1

我正在制作一个基本的 tkinter 程序,可以更改标签的文本。 当程序运行时,它已经说“你点击了按钮!”当按钮没有被点击时。请注意,我没有点击它。

此外,我尝试使用其他命令,如 print(""),但程序打印了在程序开始时自动写入的内容。

这是我的代码

from tkinter import *

Window = Tk()
Window.title("Button on Click")
Window.geometry("350x350")

Label1 = Label(Window, text="Click the button")
Label1.grid(column=2, row=1)

def Click():
   Label1.configure(text="You clicked the button!")
   Label1.grid(column=2, row=1)

Button1 = Button(Window, text="Surprise!", fg="blue", command=Click())
Button1.grid(column=1, row=1)

Window.mainloop()
python tkinter raspberry-pi
1个回答
2
投票

您调用了命令:

Button1 = Button(Window, text="Surprise!", fg="blue", command=Click())

我相信它应该没有括号

Button1 = Button(Window, text="Surprise!", fg="blue", command=Click)
© www.soinside.com 2019 - 2024. All rights reserved.