tkcalendar
tkcalendar copied to clipboard
Date autosubstitution
It is not clear why the initial date is automatically set when the Dateentry calendar is initialized. Date selection is expected from the user, but not from the program. If you use the 'textvariable' parameter when creating the calendar, the Entry field remains empty during initialization, but when the calendar opens up, the current date is automatically substituted again. Calendar v1.5, OS Windows 10.
This behavior is not specific to the initialization or to the use of a textvariable. When you click on the drop-down button, the entry field of the DateEntry
loses focus, which triggers the content validation. As ''
is not a valid date, the last valid date is automatically inserted in the entry. If you created the DateEntry
without specifying an initial date, then the default one is the current day. The idea is to make sure that the entry always contains a valid date when the content of the entry is retrieved.
If you want to have a blank date. Use .get() instead of .get_date(). DateEntry is also treated as Entry widget. The only difference will be the result, .get() will return a string while .get_date() returns a date.
I struggled with this briefly as well. If anyone else runs across this, perhaps this pattern would be useful to you
I use several DateEntry
widgets in a form tied to an information model consisting of several tk.StringVar
attributes.
When the form initializes, I prefer that they all be blank (or use the model's current value) instead of filling with today's date.
lbl = ttk.Label(self, text="Date sampled:")
current_value = project.sample_date.get() # this is to refresh the entry later
ent = tkcal.DateEntry(
self, textvariable=project.sample_date, date_pattern="mm/dd/yyyy"
)
project.sample_date.set(current_value)
lbl.bind("<Button-1>", lambda _: project.sample_date.set(""))
There's not an obvious way (that I'm aware of) to clear the entry once it accepts a value, so I bind a lambda to the entry's adjacent label so that the entry may be cleared by clicking the label. The behavior isn't really obvious or intuitive, but this approach seemed easier and less obtuse than adding an extra column to the parent frame for a "Clear" button.