Python Forum

Full Version: Empty default value for tkCalender
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good day how do you make TkCalendar DatePicker start with an empty default value.
I guess you want Calendar().selection_clear().

from tkinter import Tk, Button, StringVar
from datetime import date

from tkcalendar import Calendar


def calendar_selected(event):
    selected_date = date.fromisoformat(selection.get())
    print("Selected Date:", selected_date)


root = Tk()
root.title("Demo Calendar")
# setting selection to previous month
today = date.today()
if today.month == 1:
    today = today.replace(year=today.year - 1, month=12)
else:
    today = today.replace(month=today.month - 1)

# this here is used to set the selection of year, month and day
selection = StringVar(root, value=today.isoformat())

# date_pattern to iso8601
cal = Calendar(root, textvariable=selection, date_pattern="y-mm-dd")
# clear the selection of day. Month and year stays
cal.selection_clear()
# binding the virtual event to calendar_selected
cal.bind("<<CalendarSelected>>", calendar_selected)
cal.pack()

Button(root, text="Quit", command=root.destroy).pack()
root.mainloop()
(Aug-08-2023, 12:08 PM)DeaD_EyE Wrote: [ -> ]I guess you want Calendar().selection_clear().

from tkinter import Tk, Button, StringVar
from datetime import date

from tkcalendar import Calendar


def calendar_selected(event):
    selected_date = date.fromisoformat(selection.get())
    print("Selected Date:", selected_date)


root = Tk()
root.title("Demo Calendar")
# setting selection to previous month
today = date.today()
if today.month == 1:
    today = today.replace(year=today.year - 1, month=12)
else:
    today = today.replace(month=today.month - 1)

# this here is used to set the selection of year, month and day
selection = StringVar(root, value=today.isoformat())

# date_pattern to iso8601
cal = Calendar(root, textvariable=selection, date_pattern="y-mm-dd")
# clear the selection of day. Month and year stays
cal.selection_clear()
# binding the virtual event to calendar_selected
cal.bind("<<CalendarSelected>>", calendar_selected)
cal.pack()

Button(root, text="Quit", command=root.destroy).pack()
root.mainloop()

Thank you so much it worked perfectly