[ACCEPTED]-Is there a way to press a button without touching it on tkinter / python?-tkinter

Accepted answer
Score: 18

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

Score: 5

If you also want visual feedback for the 6 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 5 this example when the button has focus and 4 Enter/Return is pressed on the keyboard, the 3 button appears to be pressed, does the same 2 thing as when clicked (mouse/touch) and 1 then appears unpressed again.

More Related questions