-1

When I try to add a command to a button it runs the command without me pressing the button. How do I fix this? For example, if I did this:

import tkinter

root = tkinter.Tk()

def a():
   print("Hello")

button = tkinter.Button(root,command=a())
button.pack()
root.mainloop()

and ran it, it would execute the function a() without me pressing the button.

Liam Hall
  • 43
  • 2
  • 9

1 Answers1

1
tkinter.Button(root,command=a())

You should pass the function, not call it, so remove the ():

tkinter.Button(root,command=a)
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • 1
    thanks I heard that you can also use a lamda like this: `tkinter.Button(root, command= lambda: a())` – Liam Hall Feb 20 '21 at 21:28
  • 1
    @LiamHall You can, but why would you? You already have a function (`a`). There is no need to wrap it in another – DeepSpace Feb 20 '21 at 21:53