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__()
self
.setWindowTitle("Python ")
self
.w_width
=
400
self
.w_height
=
500
self
.setGeometry(
100
,
100
,
self
.w_width,
self
.w_height)
self
.UiComponents()
self
.show()
def
UiComponents(
self
):
head
=
QLabel("Age Nearest Birthday Calculator",
self
)
head.setWordWrap(
True
)
head.setGeometry(
0
,
10
,
400
,
60
)
font
=
QFont(
'Times'
,
15
)
font.setBold(
True
)
font.setItalic(
True
)
font.setUnderline(
True
)
head.setFont(font)
head.setAlignment(Qt.AlignCenter)
color
=
QGraphicsColorizeEffect(
self
)
color.setColor(Qt.darkCyan)
head.setGraphicsEffect(color)
b_label
=
QLabel("Select Birthday",
self
)
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
))
self
.calendar
=
QCalendarWidget(
self
)
self
.calendar.setGeometry(
50
,
130
,
300
,
180
)
self
.calendar.setFont(QFont(
'Times'
,
6
))
date
=
QDate.currentDate()
self
.calendar.setMaximumDate(date)
calculate
=
QPushButton("Calculate Time",
self
)
calculate.setGeometry(
125
,
340
,
150
,
40
)
calculate.clicked.connect(
self
.calculate_action)
self
.result
=
QLabel(
self
)
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
):
birth
=
self
.calendar.selectedDate()
birth_year
=
birth.year()
birth_month
=
birth.month()
birth_day
=
birth.day()
current
=
QDate.currentDate()
current_year
=
current.year()
current_month
=
current.month()
current_day
=
current.day()
birth_date
=
datetime.date(birth_year, birth_month, birth_day)
current_date
=
datetime.date(current_year, current_month, current_day)
difference
=
current_date
-
birth_date
difference
=
difference.days
years
=
difference
/
365.2422
years
=
round
(years)
self
.result.setText("Closest Age
is
: "
+
str
(years))
App
=
QApplication(sys.argv)
window
=
Window()
sys.exit(App.
exec
())