FROM & TO date for BETWEEN query using DateEntry

Tkinter tkcalendar DateEntry
From and To date for BETWEEN query using DateEntry


Tkinter Calendar to select From-To Date Query string with maximum and minimum date & reset option


Two DateEntry calendars are used to collect From and To date and prepare the BETWEEN query. This query can be used in any application working on the Date columns.

We have used minimum and maximum date to restrict the selection of dates based on From and To date selection by user.
import tkinter  as tk 
from tkcalendar import DateEntry
from datetime import date
my_w = tk.Tk()
my_w.geometry("380x220")  
sel1,sel2=tk.StringVar(),tk.StringVar()
dt2,dt2='',''
def my_upd1(*args): # triggered when value of string varaible changes
    global dt1,dt2
    if(len(sel1.get())>3):
        dt1=cal1.get_date()
        cal2.config(mindate=dt1)
        dt1 = dt1.strftime("%Y-%m-%d") # create date type
        query.set("date BETWEEN " + dt1 + " AND " + dt2)
def my_upd2(*args):
    if(len(sel2.get())>3):
        dt2=cal2.get_date()
        cal1.config(maxdate=dt2)
        dt2= dt2.strftime("%Y-%m-%d")
        query.set("date BETWEEN " + dt1 + " AND " + dt2)
def my_reset():
    dt1,dt2='',''
    dt_today=date.today().strftime('%d-%m-%Y')
    cal1.set_date(dt_today) # todays date 
    cal2.set_date(dt_today) # todays date 
    cal1.config(maxdate=None)
    cal2.config(mindate=None)    
    query.set("date BETWEEN " + dt1 + " AND " + dt2)
l_from=tk.Label(my_w,text='From',font=18)
l_from.grid(row=0,column=0)
l_to=tk.Label(my_w,text='To',font=18)
l_to.grid(row=0,column=1)
cal1=DateEntry(my_w,selectmode='day',textvariable=sel1,date_pattern='dd-MM-yyyy')
cal1.grid(row=1,column=0,padx=15)
cal2=DateEntry(my_w,selectmode='day',textvariable=sel2,date_pattern='dd-MM-yyyy')
cal2.grid(row=1,column=1,padx=15)
b1=tk.Button(my_w,text='Reset'
    ,command=lambda:my_reset())
b1.grid(row=1,column=2)
query=tk.StringVar(value='Query here')
l1=tk.Label(my_w,textvariable=query,bg='lightgreen')
l1.grid(row=2,column=0,columnspan=3,pady=10)
sel1.trace('w',my_upd1) # on change of string variable 
sel2.trace('w',my_upd2) # on change of string variable 
my_w.mainloop()

DateEntry DateEntry mindate & maxdate tkcalendar
Projects in Tkinter
Read the list of formats used in displaying Date & time
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-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer