Open In App

PyQt5 – Calculator for Number of seconds You have Lived

Last Updated : 28 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 count of leap years and we can get the number of days and seconds between the dates. Then these days can be converted into seconds to get the total seconds count.
 

GUI Implementation Steps : 
1. Create a heading label that display the calculator name 
2. Create label to show user to select the birth date and the birth time 
3. Create a QCalendarWidget object for user to select the birth date 
4. Create a QTimeEdit object to get the birth time 
5. Create a push button to calculate the age in seconds 
6. Create a label to show the calculated age in seconds
Back-End Implementation : 
1. Make the calendar future date block i.e set current date as maximum date 
2. Add action to the push button 
3. Inside the push button action get the date from the calendar and time from the QTimeEdit 
4. Get the day, month and year from the date and hour and minute from the time edit 
5. Create a datetime object for current date and the birth date 
6. Get the difference from both the dates and get the days and seconds 
7. Convert days into seconds and add it to the original seconds 
8. Show the calculated seconds value through label 
 

Below is the implementation 
 

Python3




# 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 = 500
 
        # 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("Age in Seconds 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 Birthday and Time", self)
 
        # setting properties  label
        b_label.setAlignment(Qt.AlignCenter)
        b_label.setGeometry(50, 95, 300, 25)
        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))
 
        # getting current date
        date = QDate.currentDate()
 
        # blocking future dates
        self.calendar.setMaximumDate(date)
 
        # creating a time edit object to receive time
        self.time = QTimeEdit(self)
 
        # setting properties to the time
        self.time.setGeometry(50, 300, 300, 30)
        self.time.setAlignment(Qt.AlignCenter)
        self.time.setFont(QFont('Times', 9))
 
 
        # creating a push button
        calculate = QPushButton("Calculate Time", self)
 
        # setting geometry to the push button
        calculate.setGeometry(125, 360, 150, 40)
 
        # adding action to the calculate button
        calculate.clicked.connect(self.calculate_action)
 
        # creating a label to show percentile
        self.result = QLabel(self)
 
        # setting properties to result label
        self.result.setAlignment(Qt.AlignCenter)
        self.result.setGeometry(50, 420, 300, 60)
        self.result.setWordWrap(True)
        self.result.setStyleSheet("QLabel"
                                  "{"
                                  "border : 3px solid black;"
                                  "background : white;"
                                  "}")
        self.result.setFont(QFont('Arial', 11))
 
 
    def calculate_action(self):
 
        # getting birth date day
        birth = self.calendar.selectedDate()
 
        # getting year and month day of birth day
        birth_year = birth.year()
        birth_month = birth.month()
        birth_day = birth.day()
 
        # getting time of from the time edit
        time = self.time.time()
 
        # getting hour and seconds
        hour = time.hour()
        minute = time.minute()
 
        # getting today date
        current = QDate.currentDate()
 
        # getting year and month day of current day
        current = datetime.datetime.now()
 
 
        # converting  date into date object
        birth_date = datetime.datetime(birth_year, birth_month, birth_day, hour, minute, 0)
 
        # getting difference in both the dates
        difference = current - birth_date
 
        # getting difference in days
        days = difference.days
 
        # setting seconds
        seconds = difference.seconds
 
        # converting days into second and adding them
        seconds = seconds + days * 24 * 60 * 60
 
 
 
        # setting this value with the help of label
        self.result.setText("Age in seconds : " + str(seconds))
 
  
 
# 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

Python program to convert seconds into hours, minutes and seconds
Given an integer n (in seconds), convert it into hours, minutes and seconds. Examples: Input : 12345 Output : 3:25:45 Input : 3600 Output : 1:00:00 Approach #1 : Naive This approach is simply a naive approach to get the hours, minutes and seconds by simple mathematical calculations. C/C++ Code # Python Program to Convert seconds # into hours, minut
5 min read
Python | Pandas TimedeltaIndex.seconds
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas TimedeltaIndex.seconds attribute return the number of seconds for each element in the TimedeltaIndex object. The value returned r
2 min read
Pafy - Getting Length in seconds for each item of Playlist
In this article we will see how we get the length in seconds for each item of playlist in pafy. Pafy is a python library to download YouTube content and retrieve metadata. Pafy object is the object which contains all the information about the given video. A playlist in YouTube is a list, or group, of videos that plays in order, one video after the
2 min read
Get Seconds from timestamp in Python-Pandas
In this program our task is to create a program in python which will extract seconds from a given timestamp. We will use the s attribute of the DateTime object to extract the second.Example : If we have a data frame that contains 5 different time stamps such as: Timestamp 2012-12-11 04:12:01 2013-10-13 04:12:04 2014-12-14 04:12:05 2015-11-15 04:12:
1 min read
Python - Time Strings to Seconds in Tuple List
Given Minutes Strings, convert to total seconds in tuple list. Input : test_list = [("5:12", "9:45"), ("12:34", ), ("10:40", )] Output : [(312, 585), (754, ), (640, )] Explanation : 5 * 60 + 12 = 312 for 5:12. Input : test_list = [("5:12", "9:45")] Output : [(312, 585)] Explanation : 5 * 60 + 12 = 312 for 5:12. Method 1: Using loop + split() In thi
7 min read
Python program to calculate Date, Month and Year from Seconds
Given the number of seconds, the task is to write a Python program to calculate the date, month, and year in the format MM-DD-YYYY that have been passed from 1 January 1947. Examples Input: 0 Output: 01-01-1947 Input: 123456789 Output: 11-29-1950 Input: 9876543210 Output: 12-22-2259Approach to Calculate Date, Month, and Year from SecondsHere is the
4 min read
Python | Pandas Timedelta.seconds
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Timedelta is a subclass of datetime.timedelta, and behaves similarly. It is the Pandas equivalent of Python’s datetime.timedelta and is
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 - 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
Practice Tags :