tkcalendar DateEntry Date Picker

Tkinter tkcalendar
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 DateEntry
my_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 DateEntry
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
    l1.config(text=sel.get()) # 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

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 selection
import 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 date

l1=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 DateEntry
from  datetime import date
my_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 , day
cal.set_date(dt) # Set the selected date 
#cal.set_date('8/16/2021') # Set the local calendar format 
my_w.mainloop()

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.strftime("%d-%B-%Y") #format to change 
    l1.config(text=str) # read and display date
tkcalendar strftime format
str=dt.strftime("%d-%B-%Y") # format changed

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 DateEntry
from dateutil.relativedelta import relativedelta
from 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()
Read the list of formats used in displaying Date & time
DateEntry mindate & maxdate Calendar mindate & maxdate tkcalendar
Projects in Tkinter

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-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer