-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMICalculator.py
105 lines (89 loc) · 3.79 KB
/
BMICalculator.py
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
from tkinter import *
from tkinter import messagebox
root = Tk()
# sizing the window
root.geometry("500x400")
root.configure(bg="#c0b8b8")
root.title("BMI CALCULATOR")
font1 = ("Sans-serif", 12)
# creating necessary widgets
title_label = Label(root, text="BMI CALCULATOR", padx=30, pady=15, bg="#887a7a", font=font1)
weight_label = Label(root, text="Your weight(in Kgs): ", padx=13, pady=10, bg="#c0b8b8", font=font1)
height_label = Label(root, text="Your height(in mts): ", padx=13, pady=10, bg="#c0b8b8", font=font1)
bmi_label = Label(root, text="BMI Value: ", padx=13, pady=10, bg="#c0b8b8", font=font1)
bmi_msg = Label(root, text="BMI Category: ", padx=13, pady=10, bg="#c0b8b8", font=font1)
# creating entries
user_weight = Entry(root, width=20)
user_height = Entry(root, width=20)
bmi_entry = Entry(root, width=20, bg="#c0b8b8", font=font1)
cat_entry = Entry(root, width=30, bg="#c0b8b8", font=font1)
res = 0
msg = ''
# defining necessary functions
def calculate_bmi():
""" this function computes user's bmi """
global res, msg
msg = ""
bmi_entry.delete(0, END)
cat_entry.delete(0, END)
# bmi_msg = Label(root, padx=10, pady=30, font=font1, bg="#c0b8b8")
try:
weight = float(user_weight.get())
height = float(user_height.get())
if weight <= 0 or height <= 0:
# messagebox.showerror("Error", "Values must be greater than zero")
raise ZeroDivisionError
except ValueError:
messagebox.showerror("Error", "Values must be numeric")
res = ''
except ZeroDivisionError:
messagebox.showerror("Error", "Height and(or) weight value cannot be zero")
res = ''
else:
user_bmi = weight/(height*height)
user_bmi_res = round(user_bmi, 2)
res = str(user_bmi_res)
if user_bmi < 18.5:
msg = " \t\tUnderweight\t\t"
# bmi_msg = Label(root, text=msg, padx=10, pady=30, font=font1, bg="#c0b8b8")
cat_entry.insert(0, msg)
elif user_bmi < 25:
msg = "Normal Healthy Weight"
# bmi_msg = Label(root, text=msg, padx=10, pady=30, font=font1, bg="#c0b8b8")
elif user_bmi < 30:
msg = "\t\tOverweight\t\t"
# bmi_msg = Label(root, text=msg, padx=10, pady=30, font=font1, bg="#c0b8b8")
elif user_bmi < 35:
msg = "\t\tObesity\t\t"
# bmi_msg = Label(root, text=msg, padx=10, pady=30, font=font1, bg="#c0b8b8")
else:
msg = "\t\tSevere Obesity\t\t"
# bmi_msg = Label(root, text=msg, padx=10, pady=30, font=font1, bg="#c0b8b8")
# bmi_label = Label(root, text=res, padx=10, pady=30, font=font1, bg="#c0b8b8")
# bmi_label.grid(row=4, column=0, columnspan=2)
# bmi_msg.grid(row=5, column=0, columnspan=2)
# display_msg = str(res) + "\n" + msg
# messagebox.showinfo("BMI Category", display_msg)
bmi_entry.insert(0, res)
cat_entry.insert(0, msg)
def clear_values():
user_weight.delete(0, END)
user_height.delete(0, END)
bmi_entry.delete(0, END)
cat_entry.delete(0, END)
# creating buttons
compute_btn = Button(root, text="COMPUTE BMI", padx=5, pady=5, bg="#887a7a", font=font1, command=calculate_bmi)
reset_btn = Button(root, text="RESET", padx=5, pady=5, bg="#887a7a", font=font1, command=clear_values)
# placing them on thw window
title_label.grid(row=0, column=0, columnspan=3)
weight_label.grid(row=1, column=0)
height_label.grid(row=2, column=0)
user_weight.grid(row=1, column=1)
user_height.grid(row=2, column=1)
compute_btn.grid(row=3, column=1)
reset_btn.grid(row=3, column=0)
bmi_label.grid(row=4, column=0)
bmi_entry.grid(row=4, column=1)
bmi_msg.grid(row=5, column=0)
cat_entry.grid(row=5, column=1)
root.mainloop()