diff --git a/Age-Calculator-GUI/README.md b/Age-Calculator-GUI/README.md new file mode 100644 index 0000000..e515612 --- /dev/null +++ b/Age-Calculator-GUI/README.md @@ -0,0 +1,29 @@ +# Age Calculator GUI + +In this age calculator app, users can type in their date of birth, and the app will calculate and display their age. + +## Step 1: + +First of all, we need to import two libraries into our code. The first one is the *tkinter* library. Then, we need the *datetime* library to work with dates. + +## Step 2: + +Now, let’s create a simple window for our app and name it as *Age Calculator*. + +## Step 3: + +Then, we are going to create four labels, each for the name, year, month, and the date, and put them in the grid. + +We will create entry fields to get the user inputs corresponding to all the labels created. Put them at the right side of the corresponding labels using the *grid* method. + +## Step 4: + +Then, we are going to define a function, which will calculate the age of the user by subtracting the user’s birth date from today’s date. We name that function as `ageCalc()`. + +Then, we create a `Label` area that will display the age of the user as output in the function itself. + +## Step 5: + +Then, we are going to create a button for users to submit their input values. We link the button to the `ageCalc` function. + +Finally, let’s run everything inside the window using the `mainloop()` method. diff --git a/Age-Calculator-GUI/age_calc_gui.py b/Age-Calculator-GUI/age_calc_gui.py new file mode 100644 index 0000000..1ee3b6d --- /dev/null +++ b/Age-Calculator-GUI/age_calc_gui.py @@ -0,0 +1,63 @@ +# import libraries +from tkinter import * +from datetime import date + +# initialized window +root = Tk() +root.geometry('280x300') +root.resizable(0, 0) +root.title('Age Calculator') +statement = Label(root) + +# defining the function for calculating age + + +def ageCalc(): + global statement + statement.destroy() + today = date.today() + birthDate = date(int(yearEntry.get()), int( + monthEntry.get()), int(dayEntry.get())) + age = today.year - birthDate.year + if today.month < birthDate.month or today.month == birthDate.month and today.day < birthDate.day: + age -= 1 + statement = Label(text=f"{nameValue.get()}'s age is {age}.") + statement.grid(row=6, column=1, pady=15) + + +# creating a label for person's name to display +l1 = Label(text="Name: ") +l1.grid(row=1, column=0) +nameValue = StringVar() + +# creating a entry box for input +nameEntry = Entry(root, textvariable=nameValue) +nameEntry.grid(row=1, column=1, padx=10, pady=10) + +# label for year in which user was born +l2 = Label(text="Year: ") +l2.grid(row=2, column=0) +yearValue = StringVar() +yearEntry = Entry(root, textvariable=yearValue) +yearEntry.grid(row=2, column=1, padx=10, pady=10) + +# label for month in which user was born +l3 = Label(text="Month: ") +l3.grid(row=3, column=0) +monthValue = StringVar() +monthEntry = Entry(root, textvariable=monthValue) +monthEntry.grid(row=3, column=1, padx=10, pady=10) + +# label for day/date on which user was born +l4 = Label(text="Day: ") +l4.grid(row=4, column=0) +dayValue = StringVar() +dayEntry = Entry(root, textvariable=dayValue) +dayEntry.grid(row=4, column=1, padx=10, pady=10) + +# create a button for calculating age +button = Button(text="Calculate age", command=ageCalc) +button.grid(row=5, column=1) + +# infinite loop to run program +root.mainloop()