Tkinter date entry with boxes
Souvik Dutta
souvik.viksou at gmail.com
Fri Apr 17 10:35:14 EDT 2020
You can use 12 if statements to check for the correctness of dates. For the
year you would only be needed to check the type. To insert "/" you should
return your own data type. That is make a class that returns a string of
the format "dd/mm/yy"
using parameters of date, month and year. As for not getting the value I
think your syntax is not correct. Are you sure it would not be .getText()
or something similar.
On Fri, 17 Apr, 2020, 7:55 pm , <annunci.pervendite at gmail.com> wrote:
> Hi, I'm creating a registration form and I found this script that I'd like
> to use to have my participants to input their birth date:
>
> 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
> return all((text.isdigit(), v1 <= int(value) <= v2, len(value)
> <= l))
>
> # else return False
> return False
>
>
> And I'm using it like this:
>
> self.birthday = StringVar(self)
> self.birthday.set('')
> self.birthday=DateEntry(self, years=(1935, 2020))
> self.birthday.place(relx=0.22, rely=0.16, height=25, width=100)
>
>
> I'm having several issues and questions:
> 1. I'd like to write the complete entry date on excel but it gives me a
> format date error when I try to do so. Also when I try to
> print(self.birthday.get()) I get nothing. I can't access the value
> 2. I'd need the day, month and year also saved separately (I need the year
> to calculate the age of the participants).
> 3. Is it possible to put a little bit of space between the three boxes and
> add a "/" between them?
> 4. Lastly right now it is possible to input incorrect date like 31
> November ecc. How could I fix this?
> Thank you for your help!
> --
> https://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list