pip install tkcalendar
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()
cal=DateEntry(my_w,selectmode='day')
We can add specific date
cal=DateEntry(my_w,selectmode='day',year=2021,month=8,day=17)
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()
cal.bind('<<DateEntrySelected>>',my_upd)
Here we are not using String Variable trace method
import tkinter as tk
from tkcalendar import DateEntry
my_w = tk.Tk()
my_w.geometry("380x200")
cal=DateEntry(my_w,selectmode='day')
cal.grid(row=1,column=1,padx=20)
def my_upd(e): # triggered on select event
l1.config(text=cal.get_date()) # read and display
l1=tk.Label(my_w,bg='yellow') # Label to display date
l1.grid(row=1,column=2)
cal.bind('<<DateEntrySelected>>',my_upd)
my_w.mainloop()
# 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='date',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)
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()
Resetting to today's date
cal.set_date(date.today()) # todays date
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=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()
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-2021
str_dt2=dt.strftime("%d-%m-%Y) # 18-04-2021
str_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()cal=DateEntry(my_w,selectmode='day',date_pattern='MM-dd-yyyy')
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'
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()
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()
from datetime import datetime
import tkinter as tk
from tkcalendar import DateEntry
my_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 opening
my_w.mainloop()
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 & time01-04-2023 | |
nice guide |