Python Forum
Date entry in box format issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Date entry in box format issue
#1
Hi everyone,
I've found this code to enter the date and that I'd like to use in tkinter but I'm having several issue:
import tkinter as tk
from datetime import datetime


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) + 2, 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
This is how I'm using it after importing it

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 cannot access to the entry input I'm trying this way:
for key in self.birthday.keys():
         birth_year = self.birthday.get('year')
         birth_month = self.birthday.get('month')
         birth_day =  self.birthday.get('day')
         birth_date = datetime.datetime(birth_year, birth_month, birth_day)
But it gives me this error "birth_year = self.birthday.get('year')
TypeError: get() takes 1 positional argument but 2 were given". I've no idea why this is happening.
Also I'd like to edit a little the code and I was wondering if it could be possible to:
1. Po put a little bit of space between the three boxes and add a "/" between them?
2. Also right now it is possible to input incorrect date like 31 November ecc. How could I fix this?

Thank you all for your help!
Reply


Messages In This Thread
Date entry in box format issue - by PeroPuri - Apr-18-2020, 08:03 PM
RE: Date entry in box format issue - by joe_momma - Apr-18-2020, 09:56 PM
RE: Date entry in box format issue - by PeroPuri - Apr-19-2020, 11:26 AM
RE: Date entry in box format issue - by joe_momma - Apr-19-2020, 04:10 PM
RE: Date entry in box format issue - by PeroPuri - Apr-20-2020, 10:24 PM
RE: Date entry in box format issue - by joe_momma - Apr-21-2020, 02:26 AM
RE: Date entry in box format issue - by PeroPuri - Apr-25-2020, 11:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Entry Widget issue PA3040 16 7,209 Jan-20-2021, 02:21 PM
Last Post: pitterbrayn
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,553 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 3,574 Mar-18-2019, 05:36 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020