tkcalendar DateEntry Date Picker

At Command prompt enter pip install tkcalendar
pip install tkcalendar

Tkinter Date picker Entry box using DateEntry for user to select read and set the date from calendar
import tkinter  as tk from tkcalendar import DateEntrymy_w = tk.Tk()my_w.geometry("340x220")  cal=DateEntry(my_w,selectmode='day')cal.grid(row=1,column=1,padx=15)my_w.mainloop()
tkcalendar DateEntry
If we don't specify any year , month or day then it will show current year month and day as default selected.
cal=DateEntry(my_w,selectmode='day')
We can add specific date
cal=DateEntry(my_w,selectmode='day',year=2021,month=8,day=17)

Collecting selected date

tkcalendar DateEntry reading selection
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.
import tkinter  as tk from tkcalendar import DateEntrymy_w = tk.Tk()my_w.geometry("380x200")  sel=tk.StringVar() # declaring string variable 
cal=DateEntry(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=sel.get()) # read and display datel1=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

tkcalendar DateEntry reading 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 using get_date()
# on Button click reading and displaying date selectionimport tkinter  as tk from tkcalendar import DateEntry my_w = tk.Tk()my_w.geometry("380x220")  cal=DateEntry(my_w,selectmode='day')cal.grid(row=1,column=1,padx=20,pady=30)def my_upd(): # triggered on Button Click    l1.config(text=cal.get_date()) # read and display datel1=tk.Label(my_w,text='data',bg='yellow')  # Label to display date l1.grid(row=1,column=3)
b1=tk.Button(my_w,text='Read', command=lambda:my_upd())b1.grid(row=1,column=2)
my_w.mainloop()
We can use get() method to read the selected date. Similarly we can change the format of the date. Here is the change in code inside my_upd() function
def my_upd(): # triggered on Button Click    #l1.config(text=cal.get_date()) # read and display date    #l1.config(text=cal.get()) #  using Entry widget get()     dt=cal.get_date()    str=dt.strftime("%d-%B-%Y") # changing the format     l1.config(text=str)

setting the selected value using set_date()

tkcalendar DateEntry set_date()
We can set the default or change the selection of date to any value by using set_date(). Input should be as datetime.date or as calendar local.
import tkinter  as tk from tkcalendar import DateEntryfrom  datetime import datemy_w = tk.Tk()my_w.geometry("380x220")  cal=DateEntry(my_w,selectmode='day')cal.grid(row=1,column=1,padx=15)dt=date(2021,8,19) # specific date Year, month , daycal.set_date(dt) # Set the selected date #cal.set_date('8/16/2021') # Set the local calendar format my_w.mainloop()
Resetting to today's date
cal.set_date(date.today()) # todays date 

Deleting DateEntry

Resetting DateEntry on Button click
We can clear the date entered inside DateEntry field on click of a button.
import tkinter  as tk from tkcalendar import DateEntrymy_w = tk.Tk()my_w.geometry("380x220")  cal=DateEntry(my_w,selectmode='day')cal.grid(row=0,column=0,padx=15,pady=10)b1=tk.Button(my_w,text='Clear',font=20,command=lambda:cal.delete(0,'end'))b1.grid(row=0,column=1,padx=10)my_w.mainloop()
Tkinter clearing dateEntry inputs on Click of a button using delete method of Entry

Changing the date format

To change the return date format we used get_date(), the get_date() method return the selected date as a datetime.date instance. So we can change the format by using strftime().
In above code ( Button Click to display ) the changes inside the function my_upd() is here.
def my_upd(): # triggered on Button Click    dt=cal.get_date()    str_dt=dt.strftime("%d-%B-%Y") #format to change     l1.config(text=str_dt) # read and display date
Here are some sample outputs whene we change the format
dt=cal.get_date()  # cal is my calendar str_dt=dt.strftime("%d-%B-%Y") # date string 18-April-2021str_dt2=dt.strftime("%d-%m-%Y) # 18-04-2021str_dt3=dt.strftime("%m/%d/%Y) # 04/18/2021 str_dt4=dt.strftime("%Y-%m-%d) # 2021-04-18(For Database query) 
List of date formats we can use by strftime()

To change the display format on date selection we can use date_pattern option.
cal=DateEntry(my_w,selectmode='day',date_pattern='MM-dd-yyyy')
tkcalendar strftime format
str=dt.strftime("%d-%B-%Y") # format changed
Use four digit Year, if on selection of date is jumping to 2K years. Say on selection of 1998 your year selection is holding 2098 then use date_pattern='MM-dd-yyyy'

Difference in two calendar dates

Difference in days of two calendar dates using Tkinter tkcalendar DateEntry


User can select start date and end date from two different calendars. On click of the button the difference in days between two selected dates are displayed in a Label.
import tkinter  as tk 
from tkcalendar import DateEntry # pip install tkcalendar 
my_w = tk.Tk()
my_w.geometry("400x420") # width and height of window   
font1=['Times',56,'normal'] # font style to display output 

l1=tk.Label(my_w,text='data',bg='yellow',font=font1)  # display difference 
l1.grid(row=0,column=0,padx=10,pady=20,columnspan=3,sticky='ew')

cal1=DateEntry(my_w,selectmode='day')
cal1.grid(row=1,column=0,padx=20,pady=30)
cal2=DateEntry(my_w,selectmode='day')
cal2.grid(row=1,column=1,padx=20,pady=30)

b1=tk.Button(my_w,text='Diff in Days', bg='lightgreen',
        font=20,command=lambda:my_upd())
b1.grid(row=1,column=2)
def my_upd(): # triggered on Button Click
    diff_days=(cal2.get_date()-cal1.get_date()).days # difference in days 
    #print(diff_days)
    l1.config(text=str(diff_days)+' days') # read and display date

my_w.mainloop()

Age Calculation

Read more about relativedelta here.
User will select the date of birth from the Calendar and output in terms of year , month and days i.e Age based on today's date will be displayed.
import tkinter  as tk from tkcalendar import DateEntryfrom dateutil.relativedelta import relativedeltafrom datetime import date, datetime
my_w = tk.Tk()my_w.geometry("380x200")  sel=tk.StringVar() # declaring string variable 
cal=DateEntry(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    if(len(sel.get())>4):        l1.config(text=sel.get()) # read and display date        dob = datetime.strptime(sel.get(),'%m/%d/%y')        dt=date.today()        dt3=relativedelta(dt,dob)        l2.config(text="Dayes:" + str(dt3.days) +"\n Months:"+ str(dt3.months) + "\n Years:"+ str(dt3.years) )        print("Dayes:",dt3.days," Months:",dt3.months," Years:", dt3.years)l1=tk.Label(my_w,bg='yellow')  # Label to display date l1.grid(row=1,column=2)
l2=tk.Label(my_w)  # Label to display date l2.grid(row=1,column=3,padx=10)
sel.trace('w',my_upd) # on change of string variable my_w.mainloop()

Adding time to the Calendar

Drop down Calendar with time sliders

While selecting date from the Calendar, user can set the Hour , minute and second by using three Scales. One Date object is created by using strptime() from drop-down Calendar date ( Year-month-day ) and the values from the sliders for Hour, minute and seconds.

The display format is managed by using strftime()

Tkinter Calendar with time to set the Hour minute and second values along with date by using Scales
from datetime import datetime import tkinter  as tk from tkcalendar import DateEntrymy_w = tk.Tk()my_w.geometry("415x250")  def my_upd(*args): # triggered when value of string varaible changes    dt=sel.get()    # collect the selected date as string    if(len(dt)>5):        dt=dt + ":"+str(hr.get())+","+ str(mn.get())+","+str(sc.get())        str1=datetime.strptime(dt,'%m/%d/%y:%H,%M,%S')        str1=str1.strftime("%d-%b-%Y : %H:%M:%S") # display format         l1.config(text=str1)sel=tk.StringVar()cal=DateEntry(my_w,selectmode='day',textvariable=sel)cal.grid(row=1,column=0,padx=1,sticky='N')l1=tk.Label(my_w,bg='yellow',font=('Times',28,'normal'))# show date l1.grid(row=0,column=0,padx=5,columnspan=4)l_hr=tk.Label(my_w,text='Hour')l_hr.grid(row=1,column=1,sticky='N')   hr = tk.Scale(my_w, from_=0, to=23,    orient='vertical',length=150,command=my_upd)hr.grid(row=2,column=1)l_mn=tk.Label(my_w,text='Mintue')l_mn.grid(row=1,column=2,sticky='N')mn = tk.Scale(my_w, from_=0, to=59,    orient='vertical',length=150,command=my_upd)mn.grid(row=2,column=2)l_sc=tk.Label(my_w,text='Second')l_sc.grid(row=1,column=3,sticky='N')sc = tk.Scale(my_w, from_=0, to=59,    orient='vertical',length=150,command=my_upd)sc.grid(row=2,column=3)sel.trace('w',my_upd) # on change of string variable my_upd() # Show the date and time while openingmy_w.mainloop()

ttk.Style

DateEntry inherits from ttk.Entry therefore the styling is done using a ttk style
Use the clam them if style is not working.
import tkinter  as tk 
from tkinter import ttk
from tkcalendar import DateEntry
my_w = tk.Tk()
my_w.geometry("380x200")  
sel=tk.StringVar() # declaring string variable 
style = ttk.Style(my_w)
style.theme_use('clam') #Theme to be changed # alt , classic, clam
style.configure('my.DateEntry',
                fieldbackground='lightblue',
                background='dark green',
                foreground='dark blue',
                arrowcolor='red',
				)

cal=DateEntry(my_w,style='my.DateEntry',selectmode='day',textvariable=sel)
cal.grid(row=1,column=1,padx=20)

my_w.mainloop()
Read the list of formats used in displaying Date & time
Generating Query using FROM-TO dates DateEntry mindate & maxdate Calendar mindate & maxdate tkcalendar
Projects in Tkinter Ttkbootstrap DateEntry
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2023 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer