Is there a way to press a button without touching it on tkinter / python?

If you also want visual feedback for the button you can do something like this:

from time import sleep

# somewhere the button is defined to do something when clicked
self.button_save = tk.Button(text="Save", command = self.doSomething)

# somewhere else 
self.button_save.bind("<Return>", self.invoke_button)

def invoke_button(self, event):
    event.widget.config(relief = "sunken")
    self.root.update_idletasks()
    event.widget.invoke()
    sleep(0.1)
    event.widget.config(relief = "raised")

In this example when the button has focus and Enter/Return is pressed on the keyboard, the button appears to be pressed, does the same thing as when clicked (mouse/touch) and then appears unpressed again.


As Joel Cornett suggests in a comment, it might make more sense to simply call the callback that you passed to the button. However, as described in the docs, the Button.invoke() method will have the same effect as pressing the button (and will return the result of the callback), with the slight advantage that it will have no effect if the button is currently disabled or has no callback.