我正在制作一个基本的 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()
您调用了命令:
Button1 = Button(Window, text="Surprise!", fg="blue", command=Click())
我相信它应该没有括号
Button1 = Button(Window, text="Surprise!", fg="blue", command=Click)