« Basics of Python Tkinter « More Projects in Tkinter
At Command prompt enter pip install tkcalendar

import tkinter as tk
from tkinter import ttk
from tkcalendar import Calendar
my_w = tk.Tk()
my_w.geometry("380x200")
cal=Calendar(my_w,selectmode='day',year=2021,month=8,day=23)
cal.grid(row=1,column=1,padx=20)
my_w.mainloop()

If we don't specify any year , month or day then it will show current year month and day as default selected.
cal=Calendar(my_w,selectmode='day')
Take Date as user input through Calendar by using DateEntry→
Collecting selected date: get_date()

We will read and display the selected date from the calendar. Note that we are not using any Click event of the button here to read and display the selected date. To trigger the event we are using String Variable trace method to monitor the change in value.
get_date() : Returns us the date as string ( We can't format this, check below selection_get() )
import tkinter as tk
from tkinter import ttk
from tkcalendar import Calendar
my_w = tk.Tk()
my_w.geometry("380x200")
sel=tk.StringVar()
cal=Calendar(my_w,selectmode='day',textvariable=sel)
cal.grid(row=1,column=1,padx=20)
def my_upd(*args): # triggered when value of string varaible changes
l1.config(text=cal.get_date()) # read and display date
l1=tk.Label(my_w,bg='yellow') # Label to display date
l1.grid(row=1,column=2)
sel.trace('w',my_upd) # on change of string variable
my_w.mainloop()
Reading Selected date on Button Click
Once the button ( b1) is clicked the function my_upd() is executed and the Label text option is updated with new selection by config() method.

import tkinter as tk
from tkinter import ttk
from tkcalendar import Calendar
my_w = tk.Tk()
my_w.geometry("380x220")
cal=Calendar(my_w,selectmode='day')
cal.grid(row=1,column=1,padx=20)
def my_upd(): # triggered on Button Click
l1.config(text=cal.get_date()) # read and display date
l1=tk.Label(my_w,bg='yellow') # Label to display date
l1.grid(row=1,column=2)
b1=tk.Button(my_w,text='Read',command=lambda:my_upd())
b1.grid(row=2,column=1)
my_w.mainloop()
setting the selected value using selection_set()
We can set the default or change the selection of date to any value by using selection_set(). Input should be as datetime.date or as calendar local.
import tkinter as tk
from tkinter import ttk
from tkcalendar import Calendar
from datetime import date
my_w = tk.Tk()
my_w.geometry("380x220")
cal=Calendar(my_w,selectmode='day')
cal.grid(row=1,column=1,padx=15)
#dt=date(2021,8,19) # specific date Year, month , day
#cal.selection_set(dt) # Set the selected date
cal.selection_set('8/16/2021') # Set the local calendar format
my_w.mainloop()
Changing the date format
To change the return date format we will use selection_get(), the selection_get() method return the selected date as a datetime.date instance. So we can change the format by using strftime().
In above code the changes inside the function my_upd() is here.
def my_upd(): # triggered on Button Click
dt=cal.selection_get()
str=dt.strftime("%d-%m-%Y") # format changed
l1.config(text=str) # read and display date

str=dt.strftime("%d-%B-%Y") # format changed
« Read the list of formats used in displaying Date & time
Calendar mindate & maxdate DateEntry Task list with DateEntry
Projects in Tkinter
← Subscribe to our YouTube Channel here