Starting my adventure with python Tkinter I just tried to develop an Entry which accepts only dates. However, more than just validate the Entry I was trying to coordinate the content "appearance" by adding "/" in proper positions to have a valid date format (dd/mm/aaaa). The problem is: if I insert a char instead of another, using the Entry method or StringVar, the validate function just work no longer for any other insert in the Entry.
What is the problem with that solution? Is there a way to make it work?
Here is the code I was developing:
from tkinter import *
root = Tk()
def f(att):
print(att)
if len(att) == 0:
return True
elif len(att) == 1 and att in "123":
return True
elif len(att) == 2 and att.isdigit() and int(att) < 32:
return True
elif len(att) == 3 and att[-1] != "/":
ent.insert(END, "/")
return True
else:
return False
ent = Entry(root, validate="key")
ent["validatecommand"] = (ent.register(f), "%P")
ent.pack(pady=10, padx=10)
ent.focus()
root.mainloop()