Skip to content

Commit b9ac220

Browse files
authored
Create BMI-calculator.py
1 parent eb70737 commit b9ac220

1 file changed

Lines changed: 212 additions & 0 deletions

File tree

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
import sys
2+
import os
3+
import tkinter as tk
4+
from tkinter import messagebox
5+
6+
# =========================
7+
# THEME
8+
# =========================
9+
APP_BG = "#121212"
10+
PANEL_BG = "#1F1F1F"
11+
BTN_BG = "#2C2C2C"
12+
ACCENT = "#FF6F61"
13+
TEXT_CLR = "#E0E0E0"
14+
SUBTEXT_CLR = "#AAAAAA"
15+
INPUT_BG = "#333333"
16+
INPUT_FG = "#FFFFFF"
17+
18+
FONT = ("Segoe UI", 11)
19+
20+
# =========================
21+
# APP
22+
# =========================
23+
class BMICalculatorApp:
24+
def __init__(self, root):
25+
self.root = root
26+
root.title("MateTools – BMI Calculator")
27+
root.geometry("1000x520")
28+
root.configure(bg=APP_BG)
29+
root.resizable(False, False)
30+
31+
# =========================
32+
# LEFT PANEL
33+
# =========================
34+
left = tk.Frame(root, bg=PANEL_BG, width=420)
35+
left.pack(side="left", fill="y")
36+
37+
header = tk.Frame(left, bg=PANEL_BG)
38+
header.pack(fill="x", padx=16, pady=(18, 10))
39+
40+
tk.Label(
41+
header,
42+
text="MateTools",
43+
bg=PANEL_BG,
44+
fg=ACCENT,
45+
font=("Segoe UI", 20, "bold")
46+
).pack(side="left")
47+
48+
tk.Frame(left, bg=ACCENT, height=2).pack(fill="x", padx=16, pady=(0, 14))
49+
50+
tk.Label(
51+
left,
52+
text="BMI Calculator",
53+
bg=PANEL_BG,
54+
fg=TEXT_CLR,
55+
font=("Segoe UI", 14, "bold")
56+
).pack(anchor="w", padx=16, pady=(0, 2))
57+
58+
tk.Label(
59+
left,
60+
text="Calculate your Body Mass Index",
61+
bg=PANEL_BG,
62+
fg=SUBTEXT_CLR,
63+
font=("Segoe UI", 10)
64+
).pack(anchor="w", padx=16, pady=(0, 16))
65+
66+
tk.Frame(left, bg=BTN_BG, height=1).pack(fill="x", padx=16, pady=(0, 16))
67+
68+
# =========================
69+
# INPUT FIELDS
70+
# =========================
71+
input_frame = tk.Frame(left, bg=PANEL_BG)
72+
input_frame.pack(padx=16, pady=16, fill="x")
73+
74+
tk.Label(input_frame, text="Height (cm):", bg=PANEL_BG, fg=TEXT_CLR, font=FONT).pack(anchor="w")
75+
self.height_entry = tk.Entry(input_frame, bg=INPUT_BG, fg=INPUT_FG, font=FONT)
76+
self.height_entry.pack(fill="x", pady=(0, 10))
77+
78+
tk.Label(input_frame, text="Weight (kg):", bg=PANEL_BG, fg=TEXT_CLR, font=FONT).pack(anchor="w")
79+
self.weight_entry = tk.Entry(input_frame, bg=INPUT_BG, fg=INPUT_FG, font=FONT)
80+
self.weight_entry.pack(fill="x", pady=(0, 10))
81+
82+
# =========================
83+
# BUTTONS
84+
# =========================
85+
btn_frame = tk.Frame(left, bg=PANEL_BG)
86+
btn_frame.pack(fill="x", padx=16, pady=16)
87+
88+
def make_btn(text, cmd, color=BTN_BG):
89+
return tk.Button(
90+
btn_frame,
91+
text=text,
92+
command=cmd,
93+
bg=color,
94+
fg="white",
95+
font=("Segoe UI", 11, "bold"),
96+
relief="flat",
97+
height=2,
98+
width=18
99+
)
100+
101+
make_btn("Calculate BMI", self.calculate_bmi, ACCENT).pack(side="left", expand=True, padx=4)
102+
make_btn("About", self.show_about, BTN_BG).pack(side="left", expand=True, padx=4)
103+
104+
# =========================
105+
# RIGHT PANEL
106+
# =========================
107+
right = tk.Frame(root, bg=APP_BG)
108+
right.pack(side="right", fill="both", expand=True)
109+
110+
self.result_card = tk.Frame(right, bg=PANEL_BG)
111+
self.result_card.pack(padx=30, pady=40, fill="both", expand=True)
112+
113+
tk.Label(
114+
self.result_card,
115+
text="Your BMI Result",
116+
bg=PANEL_BG,
117+
fg=TEXT_CLR,
118+
font=("Segoe UI", 16, "bold")
119+
).pack(pady=(20, 10))
120+
121+
# BMI Value display
122+
self.bmi_value_label = tk.Label(
123+
self.result_card,
124+
text="--",
125+
bg=PANEL_BG,
126+
fg=ACCENT,
127+
font=("Segoe UI", 40, "bold")
128+
)
129+
self.bmi_value_label.pack(pady=(10, 5))
130+
131+
# BMI Status display
132+
self.bmi_status_label = tk.Label(
133+
self.result_card,
134+
text="Enter your height and weight to calculate BMI",
135+
bg=PANEL_BG,
136+
fg=SUBTEXT_CLR,
137+
font=("Segoe UI", 16, "bold")
138+
)
139+
self.bmi_status_label.pack(pady=(5, 20))
140+
141+
# BMI Progress Bar
142+
self.bmi_bar_bg = tk.Frame(self.result_card, bg="#333333", height=30)
143+
self.bmi_bar_bg.pack(fill="x", padx=40, pady=(10, 0))
144+
self.bmi_bar_fg = tk.Frame(self.bmi_bar_bg, bg=ACCENT, width=0, height=30)
145+
self.bmi_bar_fg.place(x=0, y=0)
146+
147+
# BMI labels along the bar
148+
self.bmi_labels_frame = tk.Frame(self.result_card, bg=PANEL_BG)
149+
self.bmi_labels_frame.pack(fill="x", padx=40, pady=(5, 0))
150+
tk.Label(self.bmi_labels_frame, text="Underweight", bg=PANEL_BG, fg="#87CEEB", font=("Segoe UI", 10)).pack(side="left")
151+
tk.Label(self.bmi_labels_frame, text="Normal", bg=PANEL_BG, fg="#32CD32", font=("Segoe UI", 10)).pack(side="left", expand=True)
152+
tk.Label(self.bmi_labels_frame, text="Overweight", bg=PANEL_BG, fg="#FFD700", font=("Segoe UI", 10)).pack(side="left")
153+
tk.Label(self.bmi_labels_frame, text="Obesity", bg=PANEL_BG, fg="#FF4500", font=("Segoe UI", 10)).pack(side="right")
154+
155+
# =========================
156+
# METHODS
157+
# =========================
158+
def calculate_bmi(self):
159+
try:
160+
height_cm = float(self.height_entry.get())
161+
weight_kg = float(self.weight_entry.get())
162+
163+
if height_cm <= 0 or weight_kg <= 0:
164+
raise ValueError
165+
166+
height_m = height_cm / 100
167+
bmi = weight_kg / (height_m ** 2)
168+
bmi = round(bmi, 2)
169+
170+
# Determine BMI status and color
171+
if bmi < 18.5:
172+
status = "Underweight"
173+
color = "#87CEEB"
174+
elif 18.5 <= bmi < 25:
175+
status = "Normal weight"
176+
color = "#32CD32"
177+
elif 25 <= bmi < 30:
178+
status = "Overweight"
179+
color = "#FFD700"
180+
else:
181+
status = "Obesity"
182+
color = "#FF4500"
183+
184+
# Update BMI number and status
185+
self.bmi_value_label.config(text=str(bmi), fg=color)
186+
self.bmi_status_label.config(text=status, fg=color)
187+
188+
# Update progress bar
189+
# Scale BMI from 0-40 as max width
190+
max_width = self.bmi_bar_bg.winfo_width() or 400
191+
bmi_width = min(bmi / 40, 1) * max_width
192+
self.bmi_bar_fg.config(width=bmi_width, bg=color)
193+
194+
except ValueError:
195+
messagebox.showerror("Invalid input", "Please enter valid numeric values for height and weight.")
196+
197+
def show_about(self):
198+
messagebox.showinfo(
199+
"About",
200+
"MateTools – BMI Calculator\n\n"
201+
"• Calculate Body Mass Index (BMI)\n"
202+
"• Provides BMI category with color-coded visual\n\n"
203+
"Built by MateTools"
204+
)
205+
206+
# =========================
207+
# RUN
208+
# =========================
209+
if __name__ == "__main__":
210+
root = tk.Tk()
211+
BMICalculatorApp(root)
212+
root.mainloop()

0 commit comments

Comments
 (0)