Date Calculator for adding and subtracting days Using Tkinter – Python
Prerequisite: Tkinter, tkcalendar in Tkinter, and DateTime
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications.
In this article we will see how we can create days from date calculator in Tkinter, days from date calculator is used to add or subtract the days from the selected date to get the new date. This calculator is used to get the exact date that would come after a given number of days.
Let’s Understand step by step implementation:
1. Create Normal Tkinter Window and add the calendar
Python3
# Import Required Libraryfrom tkinter import *from tkcalendar import Calendar# Create Objectroot = Tk()# Set geometryroot.geometry("400x400")# Add Calendarcal = Calendar(root, selectmode = 'day', year = 2020, month = 5, day = 22)cal.pack(pady = 20)# Execute Tkinterroot.mainloop() |
Output:

2. Add buttons and labels
Python3
# Import Required Libraryfrom tkinter import *from tkcalendar import Calendarimport datetime# Create Objectroot = Tk()# Set geometryroot.geometry("400x400")# Add Calendarcal = Calendar(root, selectmode='day', year=2020, month=5, day=22)cal.pack(pady=20)frame1 = Frame()frame2 = Frame()frame1.pack()frame2.pack()# making labelLabel(frame1, text="Days", bd=1, bg="white", width=20, relief="solid", font="italic 10 bold").pack(side=LEFT)# input for daysdays = Spinbox(frame1, from_=0, to=10000000, font="italic 10")days.pack(pady=20, padx=10)# making buttonsButton(frame2, text="Add Days", font="italic 15").pack(side=LEFT)Button(frame2, text="Subtract Days", font="italic 15").pack(padx=10)# making labelconverted_date = Label(text="Date: ", bd=2, bg="white", relief="solid", font="italic 10 bold", width=30)converted_date.pack(pady=20)# Execute Tkinterroot.mainloop() |
Output:

3. Adding functionality to buttons
Steps:
- Grab the selected date from the calendar using get_date() method.
- Convert Date into a different time format using strptime() method in datetime module
- Then we will add/subtract days from the Date.
def add_days():
date_1 = datetime.datetime.strptime(cal.get_date(), "%m/%d/%y")
end_date = date_1 + datetime.timedelta(days=int(days.get()))
converted_date.config(text=f"Date: {end_date.strftime('%m/%d/%Y')}")
def subtract_days():
date_1 = datetime.datetime.strptime(cal.get_date(), "%m/%d/%y")
end_date = date_1 - datetime.timedelta(days=int(days.get()))
converted_date.config(text=f"Date: {end_date.strftime('%m/%d/%Y')}")Below is the Implementation:-
Python3
# Import Required Libraryfrom tkinter import *from tkcalendar import Calendarimport datetime# Create Objectroot = Tk()# Set geometryroot.geometry("400x400")# Add Calendarcal = Calendar(root, selectmode = 'day', year = 2020, month = 5, day = 22)cal.pack(pady = 20)# method to add daysdef add_days(): date_1 = datetime.datetime.strptime(cal.get_date(), "%m/%d/%y") end_date = date_1 + datetime.timedelta(days=int(days.get())) converted_date.config(text=f"Date: {end_date.strftime('%m/%d/%Y')}")# method to subtract daysdef subtract_days(): date_1 = datetime.datetime.strptime(cal.get_date(), "%m/%d/%y") end_date = date_1 - datetime.timedelta(days=int(days.get())) converted_date.config(text=f"Date: {end_date.strftime('%m/%d/%Y')}")frame1 = Frame()frame2 = Frame()frame1.pack()frame2.pack()# making labelLabel(frame1, text="Days", bd=1, bg="white", width=20, relief="solid", font="italic 10 bold").pack(side=LEFT)# making spinboxdays = Spinbox(frame1, from_= 0, to = 10000000, font="italic 10")days.pack(pady=20,padx=10)# making buttonsButton(frame2, text = "Add Days", command = add_days,font="italic 15").pack(side=LEFT)Button(frame2, text = "Subtract Days", command = subtract_days,font="italic 15").pack(padx=10)# making labelconverted_date = Label(text="Date: ", bd=2, bg="white",relief="solid", font="italic 10 bold", width=30)converted_date.pack(pady=20)# Execute Tkinterroot.mainloop() |
Output:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


