# Import Required Library
from tkinter import *
from tkcalendar import Calendar
from datetime import date
# Create Object
root = Tk()
# Set geometry
root.geometry("400x400")
today = date.today() # today
maxdate=date(2023,12,31) # Maximum date in Year, month , day
# Add Calendar
cal = Calendar(root, selectmode = 'day',
year = 2020, month = 5,
day = 22,mindate=today,maxdate=maxdate)
cal.pack(pady = 20)
def grad_date():
date.config(text = "Selected Date is: " + cal.get_date())
# Add Button and Label
Button(root, text = "Get Date",
command = grad_date).pack(pady = 20)
date = Label(root, text = "")
date.pack(pady = 20)
# Execute Tkinter
root.mainloop()
for example, i wish christmas and first day of a year to be unable to be selected. (hopefully class will not be used)
*code taken from geeksfrogeeks
There is no built-in way to disable dates in
tkcalendar
but it can be added. A clean way to do so is to create a class (I know the OP would rather not use classes but I need to access some internal attributes of the calendar) inheriting fromCalendar
to add thedisable_date()
method.In
Calendar
, the days areLabel
widgets and they cannot be selected if they are indisabled
state. Therefore, the idea is to store the disabled dates in a list (self._disabled_dates
in the code below) and put the labels corresponding to these dates in the disabled state when displaying the calendar (inself._display_calendar()
method).