from
PyQt5.QtWidgets
import
*
from
PyQt5
import
QtCore, QtGui
from
PyQt5.QtGui
import
*
from
PyQt5.QtCore
import
*
import
sys
class
Window(QMainWindow):
def
__init__(
self
):
super
().__init__()
self
.setWindowTitle(
"Age calculator"
)
self
.setGeometry(
100
,
100
,
500
,
400
)
self
.UiComponents()
self
.show()
def
UiComponents(
self
):
head
=
QLabel(
"Age Calculator"
,
self
)
head.setGeometry(
100
,
10
,
300
,
60
)
font
=
QFont(
'Times'
,
14
)
font.setBold(
True
)
font.setItalic(
True
)
font.setUnderline(
True
)
head.setFont(font)
head.setAlignment(Qt.AlignCenter)
head.setStyleSheet(
"color : green;"
)
dob
=
QLabel(
"Date of Birth"
,
self
)
dob.setGeometry(
0
,
80
,
250
,
50
)
dob.setAlignment(Qt.AlignCenter)
dob.setFont(QFont(
'Times'
,
10
))
given
=
QLabel(
"Given Date"
,
self
)
given.setGeometry(
300
,
80
,
250
,
50
)
given.setAlignment(Qt.AlignCenter)
given.setFont(QFont(
'Times'
,
10
))
self
.first
=
QDateEdit(
self
)
self
.first.setGeometry(
25
,
130
,
200
,
50
)
self
.first.setAlignment(Qt.AlignCenter)
self
.first.setFont(QFont(
'Arial'
,
9
))
self
.first.dateChanged.connect(
self
.first_action)
self
.second
=
QDateEdit(
self
)
self
.second.setGeometry(
275
,
130
,
200
,
50
)
self
.second.setAlignment(Qt.AlignCenter)
self
.second.setFont(QFont(
'Arial'
,
9
))
self
.second.dateChanged.connect(
self
.second_action)
calculate
=
QPushButton(
"Calculate"
,
self
)
calculate.setGeometry(
200
,
210
,
100
,
40
)
color
=
QGraphicsColorizeEffect()
color.setColor(Qt.darkGreen)
calculate.setGraphicsEffect(color)
calculate.clicked.connect(
self
.find_age)
self
.result
=
QLabel(
self
)
self
.result.setGeometry(
50
,
280
,
400
,
80
)
self
.result.setAlignment(Qt.AlignCenter)
self
.result.setFont(QFont(
'Times'
,
12
))
self
.result.setStyleSheet(
"QLabel"
"{"
"border : 2px solid black;"
"background : lightgrey;"
"}"
)
self
.result.setWordWrap(
True
)
self
.first.setDate(QDate(
2000
,
1
,
1
))
self
.second.setDate(QDate(
2020
,
1
,
1
))
def
first_action(
self
):
date
=
self
.second.date()
self
.first.setMaximumDate(date)
def
second_action(
self
):
date
=
self
.first.date()
self
.second.setMinimumDate(date)
def
find_age(
self
):
get_Qdate1
=
self
.first.date()
birth_year
=
get_Qdate1.year()
birth_month
=
get_Qdate1.month()
birth_day
=
get_Qdate1.day()
get_Qdate2
=
self
.second.date()
given_year
=
get_Qdate2.year()
given_month
=
get_Qdate2.month()
given_day
=
get_Qdate2.day()
month
=
[
31
,
28
,
31
,
30
,
31
,
30
,
31
,
31
,
30
,
31
,
30
,
31
]
if
(birth_day > given_day):
given_month
=
given_month
-
1
given_day
=
given_day
+
month[birth_month
-
1
]
if
(birth_month > given_month):
given_year
=
given_year
-
1
given_month
=
given_month
+
12
calculated_day
=
given_day
-
birth_day
calculated_month
=
given_month
-
birth_month
calculated_year
=
given_year
-
birth_year
self
.result.setText(
str
(calculated_day)
+
" Day(s), "
+
str
(calculated_month)
+
" Month(s), "
+
str
(calculated_year)
+
" Year(s)"
)
App
=
QApplication(sys.argv)
window
=
Window()
sys.exit(App.
exec
())