Question: Entry
validation, to limit the character length and only allow numbers
This example implements:
- Allow only input of digits
- Limit number of digits according to
'DD', 'MM', 'YYYY'
- Auto
'<Tab>'
according the number of digits
- Allow leading zeros in
'DD', 'MM'
- Allow only ranges of numbers,
1, 31
, 1, 12
and argument years=(<from>, <to>)
- Returns the edited data as:
{'month': 3, 'day': 28, 'year': 2020}
Reference:
Note:
The validation get turned OFF, if the Entry
text get changed, either using .insert(...
, .delete(...
or <textvariable>.set(...)
.
import tkinter as tk
class DateEntry(tk.Frame):
def __init__(self, parent, **kwargs):
years = kwargs.pop('years', (1900, 9999))
super().__init__(parent, **kwargs)
vcmd = (self.register(self._validate), '%W', '%V', '%v', '%P', '%S')
for name, text, v1, v2 in (('day', 'DD', 1, 31),
('month', 'MM', 1, 12),
('year', 'YYYY', years[0], years[1])):
e = tk.Entry(self, name=name, width=len(text) + 1, justify="center")
e.pack(side=tk.LEFT)
e.insert(0, text)
e._valid = (len(text), v1, v2)
e.config(validate="all", validatecommand=vcmd)
def get(self):
data = {}
for entry in [self.nametowidget(child) for child in self.children]:
text = entry.get()
data[entry.winfo_name()] = int(text) if text.isdigit() else None
return data
def _validate(self, widget, cmd, validate, value, text):
# get this entry reference
w = self.nametowidget(widget)
# Clear entry or do nothing
if cmd in ('focusin', 'forced') or value == '':
if not value.isdigit():
w.delete(0, tk.END)
# Set the 'validate' option again after edit
w.after_idle(w.config, {'validate': validate})
return True
# process key
elif cmd == 'key' and value.isdigit():
# get from this entry the valid parameter
l, v1, v2 = w._valid
# get the startswith chars if YYYY
if v1 > 1 and len(value) < l:
l2 = len(value)
v1, v2 = int(str(v1)[:l2]), int(str(v2)[:l2])
# allow leading zero in DD / MM
elif v1 == 1 and len(value) == 1 and int(value) == 0:
return True
# return True if all valid else False
valid = all((text.isdigit(), v1 <= int(value) <= v2, len(value) <= l))
if valid and len(value) == l:
self.event_generate('<Tab>', when='tail')
return valid
# else return False
return False
Usage:
class App(tk.Tk):
def __init__(self):
super().__init__()
tk.Button(self, text='print date', command=self.print_date).grid()
self.date_entry = DateEntry(self, years=(2000, 2020))
self.date_entry.grid()
def print_date(self):
print('print_date: {}'.format(self.date_entry.get()))
# >>> print_date: {'month': 3, 'day': 28, 'year': 2020}
if __name__ == "__main__":
App().mainloop()
Tested with Python: 3.5 - 'TclVersion': 8.6 'TkVersion': 8.6