Search code examples
matplotlibtkintertkcalendar

Tkinter Date Picker


I am working on a program using Tkinter. I would like to create GUI to select Start date and End date and create a graph based on that. As of now, I just have a simple graph using this code. Attached is my csv file for which I want to create a graph from start date to end date.For example, I want to create graph from 13th August to 15th August. Any help is highly appreciated.

enter image description here

import pandas
import matplotlib.pyplot as plt
from tkinter import *
from tkcalendar import *

PATH_CSV_FILE = f"Daily Sales\\daywisesale.csv"

data = pandas.read_csv(PATH_CSV_FILE)
plt.plot(data["Date"], data["Price"],color='red',marker='o',linestyle='--')


plt.xlabel("Date of Sale",fontsize=12)
plt.ylabel("Daily Sale Price",fontsize=14)
plt.title("Daily Sales Report Graph")
plt.grid(True,color='b')
plt.xticks(rotation=20)
plt.show()

Solution

  • First you have to install tkcalendar by saying this, in your terminal:

    pip install tkcalendar
    

    Here is a simple example on tkcalendars DateEntry:

    from tkinter import *
    from tkinter import ttk
    from tkcalendar import DateEntry
    
    root = Tk()
    
    e7 = DateEntry(root, values="Text", year=2020, state="readonly", date_pattern="yyyy-mm-dd")
    e7.grid(row=1, column=1, padx=20, pady=5, sticky=W)
    
    root.mainloop()
    

    You could also use tkcalendars Calendar:

    from tkcalendar import Calendar
    
    e7 = Calendar(root, values="Text", year=2020, state="readonly", date_pattern="yyyy-mm-dd")
    e7.grid(row=1, column=1, padx=20, pady=5, sticky=W)
    

    For your case, you can use two of any of these widgets to get (e7.selection_get()) the start date and the end date and then work with it.

    Here is the documentation for more information on the widget

    Hope it cleared your doubts, if you have errors, do let me know

    Cheers