Python Forum
Empty default value for tkCalender
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Empty default value for tkCalender
#1
Good day how do you make TkCalendar DatePicker start with an empty default value.
Reply
#2
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()
Larz60+ likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unexplained issue with tkcalender module. cybertooth 8 7,679 Aug-12-2021, 11:00 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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