Search code examples
pythontkinter

Button Widget in Tkinter executing code when not clicked


I was making a basic tkinter program that would change the text of a label. When the program was run, it already said "You clicked the button!" while the button was not clicked. Note that I did not click it.

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

Also, I tried using other commands like print("") but the program printed whatever was written automatically at the start of the program.

Here's my code

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()

Solution

  • you called the command at

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

    i believe it should be without the brackets

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