Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading