-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathageCalcGUI.py
More file actions
160 lines (126 loc) · 5.53 KB
/
ageCalcGUI.py
File metadata and controls
160 lines (126 loc) · 5.53 KB
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
#Jesse A. Jones
#Version: 2022-12-22.2
from tkinter import *
import math
import time
import datetime
import dateHandling
import metricTime
import leapDetect
#This class makes a GUI program that is used to calculate
# the age of something based on a start date and end date.
class AgeCalc(object):
#Sets up window and necessary buttons.
def __init__(self, window = None):
self.window = window
self.frameTop = Frame(self.window)
self.frameTop.pack(side = TOP)
self.quitButton = Button(self.frameTop, text = "Quit",
font = "Ariel 20", command = self.quitButtonAction)
self.quitButton.pack()
self.frameBottom = Frame(self.window)
self.frameBottom.pack(side = BOTTOM)
self.messageI = Label(self.frameBottom, text = "Enter Birth Year:", font = "Times 20")
self.messageI.grid(row = 0, column = 0)
self.yearB = Entry(self.frameBottom, font = "Times 20")
self.yearB.grid(row = 0, column = 1)
self.messageII = Label(self.frameBottom, text = "Enter Birth Month:", font = "Times 20")
self.messageII.grid(row = 2, column = 0)
self.monthB = Entry(self.frameBottom, font = "Times 20")
self.monthB.grid(row = 2, column = 1)
self.messageIII = Label(self.frameBottom, text = "Enter Birth Day:", font = "Times 20")
self.messageIII.grid(row = 3, column = 0)
self.dayB = Entry(self.frameBottom, font = "Times 20")
self.dayB.grid(row = 3, column = 1)
self.messageIV = Label(self.frameBottom, text = "Enter Current Year:", font = "Times 20")
self.messageIV.grid(row = 4, column = 0)
self.yearC = Entry(self.frameBottom, font = "Times 20")
self.yearC.grid(row = 4, column = 1)
self.messageV = Label(self.frameBottom, text = "Enter Current Month:", font = "Times 20")
self.messageV.grid(row = 5, column = 0)
self.monthC = Entry(self.frameBottom, font = "Times 20")
self.monthC.grid(row = 5, column = 1)
self.messageVI = Label(self.frameBottom, text = "Enter Current Day:", font = "Times 20")
self.messageVI.grid(row = 6, column = 0)
self.dayC = Entry(self.frameBottom, font = "Times 20")
self.dayC.grid(row = 6, column = 1)
self.convButton = Button(self.frameBottom, text = "Calculate Age",
font = "Times 20", command = self.ageCalc)
self.convButton.grid(row = 7, column = 0)
self.cOutput = Label(self.frameBottom, text = "",
font = "Times 20", justify = LEFT)
self.cOutput.grid(row = 7, column = 1)
self.cOutputII = Label(self.frameBottom, text = "",
font = "Times 20", justify = LEFT)
self.cOutputII.grid(row = 8, column = 1)
self.cOutputIII = Label(self.frameBottom, text = "",
font = "Times 20", justify = LEFT)
self.cOutputIII.grid(row = 9, column = 1)
#Quits program.
def quitButtonAction(self):
self.window.destroy()
#Displays age calculation information.
def ageCalc(self):
self.age_calc()
self.cOutput["text"] = "Your age is: " + str(self.AGE) + " Year" + self.PII
self.cOutputII["text"] = "OR"
self.cOutputIII["text"] = str(self.ageAlt) + " Year" + self.PI + " and " + str(self.ageDay) + " day" + self.PIII
#Calculates age based on input dates.
def age_calc(self):
dateGet = dateHandling.GetDate()
dayNumFind = metricTime.MetricTime()
isLeap = leapDetect.IsLeap()
#Fetches birth date from input boxes.
yearB = dateGet.getYear(self.yearB.get())
monthB = dateGet.getMonth(self.monthB.get())
dayB = dateGet.getDay(self.dayB.get())
#Fetches "current" date from input boxes.
yearC = dateGet.getYear(self.yearC.get())
monthC = dateGet.getMonth(self.monthC.get())
dayC = dateGet.getDay(self.dayC.get())
#Converts dates to current day within year.
birthDayNumber = dayNumFind.findDayNumOfYear(yearB, monthB, dayB)
currentDayNumber = dayNumFind.findDayNumOfYear(yearC, monthC, dayC)
#div is determined to be 365 or 366 days.
div = 365 + isLeap.isLeapYear(yearB)
#Calculates decimal age and years and days old separately.
yearsOld = yearC - yearB
if currentDayNumber > birthDayNumber:
Age_day = currentDayNumber - birthDayNumber
if currentDayNumber < birthDayNumber:
Age_day = (currentDayNumber - birthDayNumber) + div
yearsOld -= 1
if currentDayNumber == birthDayNumber:
Age_day = 0
decimalAge = (currentDayNumber - birthDayNumber) / div
age = yearsOld + decimalAge
age = round(age, 3)
#Handles case of plural years and days vs not doing that.
PI = ""
if yearsOld == 0 or yearsOld > 1 or yearsOld < -1:
PI = "s"
else:
PI = ""
if age == 0 or age > 1 or age < -1 or 0 < age < 1:
PII = "s"
else:
PII = ""
if Age_day == 0 or Age_day > 1 or Age_day < -1:
PIII = "s"
else:
PIII = ""
#Sets instance variables to local variables for displaying age.
self.AGE = age
self.PII = PII
self.ageAlt = yearsOld
self.ageDay = Age_day
self.PIII = PIII
self.PI = PI
return
def main():
root = Tk()
root.title("Age Calculator")
age = AgeCalc(root)
root.mainloop()
if __name__ == "__main__":
main()