I'm trying to implement a function where a tk-Button xor Keyboard Button Press are running the same function.
Here is a code example:
from tkinter import *
root = Tk()
root.geometry("800x600")
root.title("Event Testing")
def on_closing():
root.destroy()
root_button = Button(root, text="Exit", command=on_closing)
root_button.place(x=400, y=300, anchor=CENTER)
# root.bind("<Escape>", on_closing)
root.mainloop()
When i bind the <Escape> Button and press it following Error raises:
TypeError: on_closing() missing 1 required positional argument: 'event'
If i put an event variable into the function like def on_closing(event) the <Escape> Button works but the tk-Button is raising the same Error again.
Is there a better alternative then binding <Button-1> to the tk-Button or creating one function with an event variable and one without and splitting this up.
EDIT:
I've think i found a bit ugly but functioning workaround.
root_button = Button(root, text="Exit", command=lambda: on_closing(""))
If there is still a better way to do it i would like to hear it ;).