Open In App

PyQt5 – Days From Date Calculator

Last Updated : 24 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In this article we will see how we can create a days from date calculator in PyQt5, 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 which would come after given number of days. Below is how the calculator will like

PyQt5 is cross-platform GUI toolkit, a set of python bindings for Qt v5. One can develop an interactive desktop application with so much ease because of the tools and simplicity provided by this library. Below is the command to install the PyQt5

pip install PyQt5

GUI Implementation Steps :
1. Create a heading label that display the calculator name
2. Create label to show user to select the date
3. Create a QCalendarWidget object for user to select the date
4. Create a label and spin box for telling user to enter days and get the days
5. Create two push buttons for adding and subtracting the days
6. Create a label to show the calculated date

Back-End Implementation :
1. Add action to both the push button
2. Inside the add push button action get the days and call the calculate method and pass days as argument
3. Inside the subtract push button action get the days and make the days negative and call the calculate method and pass days as argument
4. Inside the calculate method get the selected date of the calendar
5. Add the passed days to the selected date
6. Show the new date with the help of result label

Below is the implementation




# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import datetime
import sys
  
  
class Window(QMainWindow):
  
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # width of window
        self.w_width = 400
  
        # height of window
        self.w_height = 530
  
        # setting geometry
        self.setGeometry(100, 100, self.w_width, self.w_height)
  
        # calling method
        self.UiComponents()
  
        # showing all the widgets
        self.show()
  
    # method for components
    def UiComponents(self):
        # creating head label
        head = QLabel("+/- Days from a date Calculator", self)
  
        head.setWordWrap(True)
  
        # setting geometry to the head
        head.setGeometry(0, 10, 400, 60)
  
        # font
        font = QFont('Times', 15)
        font.setBold(True)
        font.setItalic(True)
        font.setUnderline(True)
  
        # setting font to the head
        head.setFont(font)
  
        # setting alignment of the head
        head.setAlignment(Qt.AlignCenter)
  
        # setting color effect to the head
        color = QGraphicsColorizeEffect(self)
        color.setColor(Qt.darkCyan)
        head.setGraphicsEffect(color)
  
        # creating a label
        b_label = QLabel("Select Date", self)
  
        # setting properties  label
        b_label.setAlignment(Qt.AlignCenter)
        b_label.setGeometry(50, 100, 300, 20)
        b_label.setStyleSheet("QLabel"
                              "{"
                              "border : 1px solid black;"
                              "background : rgba(70, 70, 70, 25);"
                              "}")
        b_label.setFont(QFont('Times', 9))
  
        # creating a calendar widget to select the date
        self.calendar = QCalendarWidget(self)
  
        # setting geometry of the calendar
        self.calendar.setGeometry(50, 120, 300, 180)
  
        # setting font to the calendar
        self.calendar.setFont(QFont('Times', 6))
  
  
        # creating a label
        days_label = QLabel("Days", self)
  
        # setting geometry to the label
        days_label.setGeometry(50, 320, 147, 40)
  
        # setting alignment
        days_label.setAlignment(Qt.AlignCenter)
  
        # setting stylesheet
        days_label.setStyleSheet("QLabel"
                                 "{"
                                 "border : 2px solid black;"
                                 "background : rgba(70, 70, 70, 35);"
                                 "}")
  
        days_label.setFont(QFont('Times', 9))
  
        # creating a spin box to get the days
        self.days = QSpinBox(self)
  
        # setting geometry to the spin box
        self.days.setGeometry(203, 320, 147, 40)
  
        # setting maximum value of spin box
        self.days.setMaximum(99999999)
  
        # setting font and alignment
        self.days.setFont(QFont('Times', 9))
        self.days.setAlignment(Qt.AlignCenter)
  
  
  
        # creating a push button
        add = QPushButton("Add Days", self)
  
        # setting geometry to the push button
        add.setGeometry(80, 380, 100, 40)
  
        # adding action to the button
        add.clicked.connect(self.add_action)
  
        # adding color effect to the push button
        color = QGraphicsColorizeEffect()
        color.setColor(Qt.blue)
        add.setGraphicsEffect(color)
  
        # creating a push button
        subtract = QPushButton("Subtract Days", self)
  
        # setting geometry to the push button
        subtract.setGeometry(220, 380, 100, 40)
  
        # adding action to the  button
        subtract.clicked.connect(self.subtract_action)
  
        # adding color effect to the push button
        color = QGraphicsColorizeEffect()
        color.setColor(Qt.red)
        subtract.setGraphicsEffect(color)
  
  
        # creating a label to show result
        self.result = QLabel(self)
  
        # setting properties to result label
        self.result.setAlignment(Qt.AlignCenter)
  
        # setting geometry
        self.result.setGeometry(50, 440, 300, 60)
  
        # making it multi line
        self.result.setWordWrap(True)
  
        # setting stylesheet
        # adding border and background
        self.result.setStyleSheet("QLabel"
                                  "{"
                                  "border : 3px solid black;"
                                  "background : white;"
                                  "}")
  
        # setting font
        self.result.setFont(QFont('Arial', 11))
  
    # method called by the add push button
    def add_action(self):
  
        # get the days from the spin box
        days = self.days.value()
  
        # call the calculate action
        self.calculate(days)
  
  
    # method called by the subtract push button
    def subtract_action(self):
  
        # get the days from the spin box
        # make the days value negative
        days = 0 - self.days.value()
  
        # call the calculate action
        self.calculate(days)
  
  
  
    def calculate(self, days):
  
        # get the selected date of calendar
        selected_date = self.calendar.selectedDate()
  
        # adding days to the selected days
        new_date = selected_date.addDays(days)
  
        # showing this date through label
        self.result.setText("Date : " + new_date.toString(Qt.ISODate))
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())


Output :



Similar Reads

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 a
3 min read
How to print date starting from the given date for n number of days using Pandas?
In this article, we will print all the dates starting from the given date for n number days. It can be done using the pandas.date_range() function. This function is used to get a fixed frequency DatetimeIndex. Syntax: pandas.date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, closed=None, **kwargs) Approac
2 min read
How to add Days to a Date in Python?
Python provides an in-built module datetime which allows easy manipulation and modification of date and time values. It allows arithmetic operations as well as formatting the output obtained from the DateTime module. The module contains various classes like date, time, timedelta, etc. that simulate the easy implementation of dates and time (month,
2 min read
Building Calculator using PyQt5 in Python
In this article we will see how we can create a calculator using PyQt5,A calculator is something used for making mathematical calculations, in particular a small electronic device with a keyboard and a visual display. below is the how the calculator will looks like GUI implementation steps Create a label to show the numbers and the output and set i
5 min read
PyQt5 - Flames Calculator
In this article we will see how we can create a flames calculator using PyQt5. This flames calculator assesses and predicts the outcome of a relationship based on an algorithm of two given names.FLAMES is a popular game named after the acronym: Friends, Lovers, Affectionate, Marriage, Enemies, Sibling. This game does not accurately predict whether
11 min read
PyQt5 - Calculator for Number of seconds You have Lived
In this article we will see how we can create calculator which tells how many seconds a person has lived. Below si how the calculator will look like Concept : We can get the age in seconds by getting the difference between the birth date and the birth time with the present date and time with the help of python datetime objects, which will keep coun
4 min read
PyQt5 - Percentile Calculator
In this article we will see how we can create a percentile calculator using PyQt5, below is how the percentile calculator will look like GUI implementation steps 1. Create a heading label that display the calculator name 2. Create label and line edit pair for total students, label to show what user has to enter and line edit to enter text 3. Simila
4 min read
PyQt5 - Age Nearest Birthday Calculator
In this article we will see how we can create a age nearest birthday calculator. Age nearest calculator is the calculator which is used to get the age nearest to the birth date. Below is how the age nearest calculator looks like Concept : We can get the age nearest birthday by getting the difference between the birth date and the present date with
4 min read
PyQt5 - Average Speed Calculator
In this article we will see how we can create a average speed calculator using PyQt5. The average speed of an object is the total distance traveled by the object divided by the elapsed time to cover that distance, below is how the average speed calculator will look like Concept : We will get the time in the form of hours and minutes, and distance i
4 min read
PyQt5 - Beats and Breaths Calculator
In this article we will see how we can create a heart beats and breaths calculator using PyQt5. Below is how the calculator will look like. A normal resting heart rate for adults ranges from 60 to 100 beats per minute. Generally, a lower heart rate at rest implies more efficient heart function and better cardiovascular fitness. For example, a well-
5 min read
Practice Tags :