DateEntry mindate & maxdate

Tkinter tkcalendar DateEntry
In our DateEntry we can use the option mindate and maxdate to disable any dates before mindate and after maxdate.
Here from 3rd Aug 2021 ( minimum date ) to 25th Aug 2021 ( maximum date ) are available for selection.
DateEntry mindate and maxdate

DateEntry Maxdate Mindate options to dynamically change the available date range in Tkinter GUI
import tkinter  as tk 
from tkcalendar import DateEntry
from datetime import date
my_w = tk.Tk()
my_w.geometry("320x210")  

dt1=date(2021,8,3) # Minimum date for selection 
dt2=date(2021,8,25) # Maximum date for selection
cal=DateEntry(my_w,selectmode='day', mindate=dt1, maxdate=dt2)
cal.grid(row=1,column=1,padx=15)

my_w.mainloop()

Today's Date with mindate & maxdate

We can allow to select all future dates including today by using mindate option.
import tkinter  as tk 
from tkcalendar import DateEntry
from datetime import datetime
my_w = tk.Tk()
my_w.geometry("320x210")  
date_today = datetime.now() # today's date 

cal=DateEntry(my_w,selectmode='day', mindate=date_today)
cal.grid(row=1,column=1,padx=15)

my_w.mainloop()
We can disable selection of all future dates by using maxdate option
cal=DateEntry(my_w,selectmode='day', maxdate=date_today)

Using timedelta

Select all days till tomorrow ( use timedelta )
from datetime import timedelta,date,datetime
dt = date.today() + timedelta(days=1) # Tomorrow 
cal=DateEntry(my_w,selectmode='day',maxdate=dt)
Select all days starting from yesterday
dt = date.today() + timedelta(days=-1) # Yesterday 
cal=DateEntry(my_w,selectmode='day',mindate=dt)

Using relativedelta

More about relativedelta.
The variable dt can be used to set mindate or maxdate based on requirment
from dateutil.relativedelta import relativedelta
from datetime import date
dt = date.today() + relativedelta(days=1))# tomorrow  
Check how future dates are enabled using relativedelta.
import tkinter  as tk 
from tkcalendar import DateEntry
from dateutil.relativedelta import relativedelta
from datetime import date
my_w = tk.Tk()
my_w.geometry("320x210")  
dt = date.today() + relativedelta(years=1,months=2,days=3) 
cal=DateEntry(my_w,selectmode='day',maxdate=dt,mindate=date.today())
cal.grid(row=1,column=1,padx=15)

my_w.mainloop()

Dynamic minimum date and maximum date

Once the user clicks a date, it became minimum date ( or start date ) and 10 days after this date became the maximum date ( or end date ). Read more about timedelta() and other data parameters to add or subtract.

Add one Reset button to restore the initial stage so user can select again.
import tkinter  as tk 
from tkcalendar import DateEntry
from datetime import timedelta,date,datetime
my_w = tk.Tk()
my_w.geometry("380x220")  
sel=tk.StringVar()
def my_upd(*args): # triggered when value of string varaible changes
    dt=sel.get()#cal.selection_get() # minmum date
    #print(dt + ": "+str(len(dt))) 
    if(len(dt)>3): # when dt is having date selection
        dt1 = datetime.strptime(dt,'%m/%d/%y') # create date type
        dt2=dt1 + timedelta(days=10) # maximum date, added 10 days
        cal.config(mindate=dt1) # set minimum date
        cal.config(maxdate=dt2) # set maximum date 
cal=DateEntry(my_w,selectmode='day',textvariable=sel)
cal.grid(row=1,column=1,padx=15)

b1=tk.Button(my_w,text='Reset'
    ,command=lambda:cal.config(mindate=None,maxdate=None))
b1.grid(row=1,column=2)

sel.trace('w',my_upd) # on change of string variable 
my_w.mainloop()

Reset to today's date

We are resetting the maxdate and mindate on Click of a button. We can trigger a function and then reset the maxdate , mindate and also reset the calendar to current date. Here is the code inside the function reset()
def reset():
    date_today = datetime.now() # today's date 
    cal.set_date(date_today) # before resetting min & max date
    cal.config(mindate=None) # reset mindate 
    cal.config(maxdate=None) # reset maxdate 
We can modify the button code to trigger the above function
b1=tk.Button(my_w,text='Reset',command=lambda:reset())
Here we are first setting the calendar default date to today and then resetting the maxdate, mindate. If you change the order then the callback function my_upd will be executed ( as we are using trace() method of string variable ) and change the mindate, maxdate to new value.
DateEntry Calendar mindate & maxdate tkcalendar
Projects in Tkinter
Read the list of formats used in displaying Date & time

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