Skip to content
Related Articles

Related Articles

Dog Years Calculator using PyQt5
  • Last Updated : 01 Aug, 2020

In this article, we will see how we can create a dog year calculator using PyQt5. Dog year calculator will tell the age of a dog if the dog was human, the dog grew a lot faster than humans. Below is how the calculator will look like :

PyQt5 is a 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 displays the calculator name. 
  2. Create a label to show users to enter the age of the dog. 
  3. Create a QSpinBox object for the user to enter the years. 
  4. Create push-button for calculating the age. 
  5. Create a label to show the calculated age.

Back-End Implementation : 

  1. Add an action to the push button. 
  2. Set minimum and maximum values to the spin box. 
  3. Inside the push button, an action gets the value of spin box. 
  4. If the value is 1 then age is 15, if the value is 2 then age is 24 else age is increment by 4 for next years. 
  5. Show the calculated age with the help of a label. 

Below is the implementation 


# importing required libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import datetime
import sys
# window class
class Window(QMainWindow):
    # Constructor 
    def __init__(self):
        # setting title of the window
        self.setWindowTitle("Python ")
        # width of the window
        self.w_width = 400
        # height of the window
        self.w_height = 400
        # setting geometry of the window
        self.setGeometry(100, 100, self.w_width, self.w_height)
        # method calling
        # showing all the widgets
    # method for components creation
    def UiComponents(self):
        # creating head label
        head = QLabel("Dog Age Calculator", self)
        # setting geometry of the head
        head.setGeometry(0, 10, 400, 60)
        # font work 
        font = QFont('Times', 15)
        # setting font to the head
        # setting alignment of the head
        # setting color effect to the head
        color = QGraphicsColorizeEffect(self)
        # creating a label
        age_label = QLabel("Age of Dog ", self)
        # setting geometry to the label
        age_label.setGeometry(50, 120, 147, 40)
        # setting alignment
        # setting stylesheet
                                 "border : 2px solid black;"
                                 "background : rgba(70, 70, 70, 35);"
        age_label.setFont(QFont('Times', 9))
        # creating a spin box
        self.age = QSpinBox(self)
        # setting geometry to the spin box
        self.age.setGeometry(203, 120, 147, 40)
        # setting maximum value of spin box
        # setting minimum value of spin box
        # setting suffix to the spin box
        self.age.setSuffix(" year(s)")
        # setting font and alignment
        self.age.setFont(QFont('Times', 9))
        # creating a push button
        calculate = QPushButton("Calculate Age", self)
        # setting geometry to the push button
        calculate.setGeometry(100, 200, 200, 40)
        # adding action to the button
        # adding color effect to the push button
        color = QGraphicsColorizeEffect()
        # creating a label to show result
        self.result = QLabel(self)
        # setting properties to result label
        # setting geometry
        self.result.setGeometry(50, 280, 300, 70)
        # making it multi line
        # setting stylesheet
        # adding border and background
                                  "border : 3px solid black;"
                                  "background : white;"
        # setting font
        self.result.setFont(QFont('Arial', 11))
    # method for calculating the dog's age
    def calculate(self):
        # getting the spin box value
        value = self.age.value()
        # if value is 1
        if value == 1:
            # dog age is 15
            d_age = 15
        # if value is 2
        elif value == 2:
            # dog age is 24
            d_age = 24
        # else dog age get incremented by 4
            d_age = 24 + (value - 2) * 4
        # showing age through label
        self.result.setText("If your dog were a human, it would be : " +str(d_age) + " years old !")
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :