j4321 / tkcalendar

Calendar widget for Tkinter
https://pypi.python.org/pypi/tkcalendar
GNU General Public License v3.0
97 stars 33 forks source link

DateEntry date_pattern parsing in yyyyMMdd format fails #85

Open skmagiik opened 2 years ago

skmagiik commented 2 years ago

DateEntry(prodfrm, width= 16,bd=2, date_pattern='yyyyMMdd')

Other variants attempted: DateEntry(prodfrm, width= 16,bd=2, date_pattern='yyyy-MMdd') DateEntry(prodfrm, width= 16,bd=2, date_pattern='yyyyMM-dd')

Succeeds: DateEntry(prodfrm, width= 16,bd=2, date_pattern='yyyy-MM-dd')

Error message:

Exception in Tkinter callback Traceback (most recent call last): File "C:\Python38\lib\tkinter__init.py", line 1883, in call__ return self.func(*args) File "C:\Python38\lib\site-packages\tkcalendar\dateentry.py", line 246, in _on_b1_press self.drop_down() File "C:\Python38\lib\site-packages\tkcalendar\dateentry.py", line 331, in drop_down date = self.parsedate(self.get()) File "C:\Python38\lib\site-packages\tkcalendar\calendar.py", line 1229, in parse_date day = int(numbers[indexes['D']]) IndexError: list index out of range

skmagiik commented 2 years ago

This appears to be an issue because the calendar parse date method is looking for regex groupings instead of matching the particular pattern exactly. Perhaps this could be improved by reading the length of digits == length of the number of letters matching (YY vs YYYY) and using those values instead of just the groupings.

skmagiik commented 2 years ago

Alternatively, you can also use python tools to accomplish this via strptime: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

Formatting would be different, but could a good solution that is versatile without much effort required. The downside of this is combability breaking for users who update the library to use in existing projects.