Welcome folks today in this post we will be building a birthday age calculator
gui desktop app using pyqt5 in python. All the full source code of the application is given below.
Get Started
In order to get started you need to install the following library using the pip
command as shown below
pip install pyqt5
After installing this library we need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# 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 Nearest Birthday 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", self) # setting properties label b_label.setAlignment(Qt.AlignCenter) b_label.setGeometry(50, 105, 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, 130, 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 push button calculate = QPushButton("Calculate Time", self) # setting geometry to the push button calculate.setGeometry(125, 340, 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, 400, 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 today date current = QDate.currentDate() # getting year and month day of current day current_year = current.year() current_month = current.month() current_day = current.day() # coverting dates into date object birth_date = datetime.date(birth_year, birth_month, birth_day) current_date = datetime.date(current_year, current_month, current_day) # getting difference in both the dates difference = current_date - birth_date # getting days from the difference difference = difference.days # getting years from the difference years = difference / 365.2422 # getting round value of years years = round(years) # setting this value with the help of label self.result.setText("Closest Age is : " + str(years)) # create pyqt5 app App = QApplication(sys.argv) # create the instance of our Window window = Window() # start the app sys.exit(App.exec()) |
Now just execute the above script by typing the below command as shown below
python app.py