2

Using dateentry from the tkcalendar python module, every time I select a date on the widget it shortens it to the m/d/yy format. I'd like it to expand to mm/dd/yyyy if possible, or at least for the year.

When using the set_date method, it will display whatever is inputted (string), but then reverts back to the shortened format once another date is clicked.

Is there any way to have it always display the full date? I can't seem to find a format parameter that would allow me to use %Y so that's why I ask.

j_4321
  • 15,431
  • 3
  • 34
  • 61
John A
  • 23
  • 1
  • 1
  • 3

2 Answers2

7

This is possible without overriding _select since version 1.5.0, using the date_pattern argument (See documentation):

import tkinter as tk
from tkcalendar import DateEntry

window = tk.Tk()

DateEntry(window, locale='en_US', date_pattern='mm/dd/y').pack()  # custom formatting
DateEntry(window, locale='en_US').pack()  # default formatting
window.mainloop()

gives

screenshot

j_4321
  • 15,431
  • 3
  • 34
  • 61
  • thank you so much. This answer is shorter and clearer. – Barbaros Özhan Jun 18 '20 at 18:21
  • FYI the `date_pattern` option wasn't available yet when Henry Yik's answer was posted. – j_4321 Jun 23 '20 at 07:40
  • Hi Juliette. Indeed I can run my own app. at the design time, but cannot generate an .exe file through use of `pyinstaller.exe` if I include `DateEntry` within the code. Do you have an advice ..? – Barbaros Özhan Jun 23 '20 at 07:56
  • @BarbarosÖzhan Have you tried the solution in the [How Tos](https://tkcalendar.readthedocs.io/en/stable/howtos.html#pyinstaller)? – j_4321 Jun 23 '20 at 08:39
  • 1
    @BarbarosÖzhan You have to use either `import babel.numbers` in main code or `--hidden-import=babel.numbers` with pyinstaller – Delrius Euphoria Oct 27 '20 at 18:05
4

If you just want to change how it appears upon selection, you can make a class that inherit DateEntry and override the _select method.

from tkcalendar import DateEntry
import tkinter as tk

root = tk.Tk()

class CustomDateEntry(DateEntry):

    def _select(self, event=None):
        date = self._calendar.selection_get()
        if date is not None:
            self._set_text(date.strftime('%m/%d/%Y'))
            self.event_generate('<<DateEntrySelected>>')
        self._top_cal.withdraw()
        if 'readonly' not in self.state():
            self.focus_set()

entry = CustomDateEntry(root)
entry._set_text(entry._date.strftime('%m/%d/%Y'))
entry.pack()

root.mainloop()
Henry Yik
  • 22,275
  • 4
  • 18
  • 40