I have a python code using the Tkinter module, the problem is that I added a validation to an entry and the callback is only executed once, even though it should be executed every time a key event occurs:
#Callback
def isNumber():
print("Callback")
root=tk.Tk()
#Entry con la validacion
entry1=tk.Entry(root, width=30, validate="key", validatecommand=(isNumber))
entry1.pack(pady=20)
#Entry normal sin validación, solo para probar eventos de Foco
entry2=tk.Entry(root, width=10)
entry2.pack()
root.mainloop()
First of all controls such as text boxes or a
tk.Entry
, require a string variable to modify or evaluate a certain function, so passing a string variable or atk.StringVar()
can make the function execute repeatedly since itvalidatecommand
only calls the function only once since it doesn't have a variable. Using the methodtrace()
on the variable, you can somehow keep track of the validation when executingvalidatecommand
on thetk.Entry
.This would be the solution: