在本文中,我们将探讨如何创建一个“最近生日年龄计算器”。这个计算器的作用是根据出生日期计算最接近当前年龄的数值。下面就是这个计算器的样子:
核心概念:
我们可以通过计算出生日期和当前日期之间的差值得出“最近生日年龄”。借助 Python 的 datetime 对象,我们可以自动处理闰年的问题,并计算出两个日期之间的天数。然后,我们将这些天数除以 365.24222(考虑到回归年的精确长度)来得到年份,最后对年份进行四舍五入,即可得到最近生日年龄。
> GUI 实现步骤:
> 1. 创建一个标题标签,用于显示计算器的名称。
> 2. 创建一个标签,提示用户选择出生日期。
> 3. 创建一个 QCalendarWidget 对象,供用户选择出生日期。
> 4. 创建一个按钮,用于计算最近年龄。
> 5. 创建一个标签,用于显示计算出的年龄。
后端实现步骤:
> 1. 限制日历控件未来的日期,即设置当前日期为最大可选日期。
> 2. 为按钮添加点击事件(Action)。
> 3. 在按钮的点击事件中,从日历获取出生日期和当前日期。
> 4. 从两个日期中分别获取年、月、日。
> 5. 为这两个日期分别创建 datetime.date 对象。
> 6. 计算两个日期的差值,并获取总天数。
> 7. 将天数除以 365.2422(考虑闰年)来获取总年份。
> 8. 获取年份的整数值,并通过标签显示该数值。
下面是具体的代码实现:
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 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()
# creating date object
birth_date = datetime.date(birth_year, birth_month, birth_day)
current_date = datetime.date(current.year(), current.month(), current.day())
# getting difference days
difference = current_date - birth_date
days = difference.days
# calculating years
years = days / 365.2422
# rounding off the years
age = round(years)
# showing age on the screen
self.result.setText("Nearest Age is : " + str(age))
# main
def main():
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())
if __name__ == "__main__":
main()