I am creating a date of birth entry form for my application, and want to make it so that they can only enter numbers, and to limit the numbers inputted to the maximum amount needed e.g. 2 for day. When I tried to implement this, it doesn't even let me do anything in the entry widgets. How can I limit the character length and only accept numbers?
def onlyNumbers(char):
if (len(dayE.get()) >= 2):
return False
elif (len(monthE.get()) >= 2):
return False
elif (len(yearE.get()) >= 4):
return False
elif (char is ""):
return True
else:
return char.isdigit()
dayV = tk.StringVar(self, value = "DD")
monthV = tk.StringVar(self, value = "MM")
yearV = tk.StringVar(self, value = "YYYY")
validation = self.register(onlyNumbers)
dayE = tk.Entry(self, font = BASIC_FONT, textvariable = dayV, width = 5, justify = "center")
dayE.pack()
dayE.config(validate = "key", validatecommand = (validation, "%P"))
monthE = tk.Entry(self, font = BASIC_FONT, textvariable = monthV, width = 5, justify = "center")
monthE.pack()
monthE.config(validate = "key", validatecommand = (validation, "%P"))
yearE = tk.Entry(self, font = BASIC_FONT, textvariable = yearV, width = 5, justify = "center")
yearE.pack()
yearE.config(validate = "key", validatecommand = (validation, "%P"))