0

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 ;).

1 Answer 1

1

Here is a solution which works, I used a lambda function instead of on_closing() function for root.bind(). Another option might be programming all in OOP.

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>", lambda x: root.destroy())

root.mainloop()

EDIT:

Ok, another try. With event=None, so the event is defined as default none. You don't get the Error because of the definition and it's the same function.

from tkinter import *

def on_closing(event=None):
    root.destroy()

root = Tk()
root.geometry("800x600")
root.title("Event Testing")
root.bind("<Escape>", on_closing)
    
root_button = Button(root, text="Exit", command=on_closing)
root_button.place(x=400, y=300, anchor=CENTER)

root.mainloop()
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the suggestion. Yes this would work for this examples but as i said in my question. I'm trying to implement a function where a tk-Button xor Keyboard Button Press are running the same function. In my main programm there is a more complex function that you cant simplify with lamda. In OOP is an need for an event variable too or am i wrong?
no worries.. I think you need an event anyway with tkinter because you bind an event to a function: pythontutorial.net/tkinter/tkinter-event-binding
Yeah when you binding something the event is needed. But im still not sure what this variable does. I edited my question and it works even if my given arg for the event is "" so. I will read through your link anyways maybe i will find something :).
ok, look at the second example I added, there is the event=none, so you can call the function without an event because the event is None as default. On the other side you can call the function with an event.. in this case you have your solution, isn't?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.