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

Tkinter buttons calling functions without pressing them

I’ve had this problem for a while. When I start a program, any tkinter buttons will call their functions without being pressed or anything. and after that, I cannot use the button anymore because it only works once for some reason. Can anybody help?

This is not the actual code, but it is an example:

from tkinter import *

root = Tk()

def function():
    print("activated")

button = Button(root, text="this is a button", command=function)
button.pack()

root.mainloop()

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

>Solution :

Just call the function rather than sending it to command:

from tkinter import *

root = Tk()

def function():
    print("activated")

button = Button(root, text="this is a button", command = function()) #notice the change here.
button.pack()

root.mainloop()

Just add ()

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