代码之家  ›  专栏  ›  技术社区  ›  IAMATP On Scratch

Tkinter中的按钮小工具在未单击时执行代码[重复]

  •  0
  • IAMATP On Scratch  · 技术社区  · 3 月前

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

    Picture - I didn't click the button, but it says I did

    此外,我尝试使用其他命令,如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()
    
    1 回复  |  直到 3 月前
        1
  •  3
  •   Kenzo Staelens    3 月前

    你拨打了的命令

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

    我认为应该去掉括号

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