I am trying this code. I want to use don function for bind()
and command
. It is showing don() missing 1 required positional argument: 'Event'. how to fix it
my code
from tkinter import *
root = Tk()
root.geometry("600x500")
def don(Event):
print("hello")
root.bind("<Return>", don)
btn1 = Button(root, text="check! ", command=don).pack()
root.mainloop()
The root of the problem is that a function called via bind
automatically gets an event parameter, but a function called by the command
option does not. The trick is to make a function that can be called with or without a parameter, and which doesn't use the parameter (since it isn't guaranteed to be there).
The normal way to do this is to make the event
parameter options, which is done like this:
def don(event=None):
print("hello")
This will cause event
to be set to None
if it's not passed in, which is harmless since the function doesn't use the event
parameter.