Open In App

Date Calculator for adding and subtracting days Using Tkinter – Python

Improve
Improve
Like Article
Like
Save
Share
Report

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 Library
from tkinter import *
from tkcalendar import Calendar
 
# Create Object
root = Tk()
 
# Set geometry
root.geometry("400x400")
 
# Add Calendar
cal = Calendar(root, selectmode = 'day',
            year = 2020, month = 5,
            day = 22)
 
cal.pack(pady = 20)
 
# Execute Tkinter
root.mainloop()


 
 

Output:

 

 

2. Add buttons and labels

 

Python3




# Import Required Library
from tkinter import *
from tkcalendar import Calendar
import datetime
 
# Create Object
root = Tk()
 
# Set geometry
root.geometry("400x400")
 
# Add Calendar
cal = Calendar(root, selectmode='day',
               year=2020, month=5,
               day=22)
 
cal.pack(pady=20)
 
frame1 = Frame()
frame2 = Frame()
 
frame1.pack()
frame2.pack()
 
# making label
Label(frame1, text="Days", bd=1, bg="white", width=20,
      relief="solid", font="italic 10 bold").pack(side=LEFT)
 
# input for days
days = Spinbox(frame1, from_=0, to=10000000, font="italic 10")
days.pack(pady=20, padx=10)
 
# making buttons
Button(frame2, text="Add Days", font="italic 15").pack(side=LEFT)
Button(frame2, text="Subtract Days", font="italic 15").pack(padx=10)
 
# making label
converted_date = Label(text="Date: ", bd=2, bg="white", relief="solid",
                       font="italic 10 bold", width=30)
converted_date.pack(pady=20)
 
# Execute Tkinter
root.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 Library
from tkinter import *
from tkcalendar import Calendar
import datetime
 
# Create Object
root = Tk()
 
# Set geometry
root.geometry("400x400")
 
# Add Calendar
cal = Calendar(root, selectmode = 'day',
            year = 2020, month = 5,
            day = 22)
 
cal.pack(pady = 20)
 
# method to add days
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')}")
 
# method to subtract days
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')}")
 
frame1 = Frame()
frame2 = Frame()
 
frame1.pack()
frame2.pack()
 
# making label
Label(frame1, text="Days", bd=1, bg="white", width=20, relief="solid",
      font="italic 10 bold").pack(side=LEFT)
 
# making spinbox
days = Spinbox(frame1, from_= 0, to = 10000000, font="italic 10")
days.pack(pady=20,padx=10)
 
# making buttons
Button(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 label
converted_date = Label(text="Date: ", bd=2, bg="white",relief="solid",
                       font="italic 10 bold", width=30)
converted_date.pack(pady=20)
 
# Execute Tkinter
root.mainloop()


 
 

Output:

 

 



Last Updated : 13 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads