-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmical.py
More file actions
72 lines (54 loc) · 2.47 KB
/
bmical.py
File metadata and controls
72 lines (54 loc) · 2.47 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
import tkinter as tk
from tkinter import messagebox
class BMICalculator(tk.Tk):
def __init__(self):
super().__init__()
self.title("Advanced BMI Calculator")
self.geometry("400x300")
self.create_widgets()
def create_widgets(self):
self.unit_var = tk.StringVar(value="metric")
self.unit_label = tk.Label(self, text="Choose units:")
self.unit_label.pack(pady=5)
self.metric_radio = tk.Radiobutton(self, text="Metric (kg, m)", variable=self.unit_var, value="metric")
self.metric_radio.pack()
self.imperial_radio = tk.Radiobutton(self, text="Imperial (lbs, inches)", variable=self.unit_var, value="imperial")
self.imperial_radio.pack()
self.weight_label = tk.Label(self, text="Weight:")
self.weight_label.pack(pady=5)
self.weight_entry = tk.Entry(self)
self.weight_entry.pack()
self.height_label = tk.Label(self, text="Height:")
self.height_label.pack(pady=5)
self.height_entry = tk.Entry(self)
self.height_entry.pack()
self.calculate_button = tk.Button(self, text="Calculate BMI", command=self.calculate_bmi)
self.calculate_button.pack(pady=20)
def calculate_bmi(self):
try:
weight = float(self.weight_entry.get())
height = float(self.height_entry.get())
unit = self.unit_var.get()
if weight <= 0 or height <= 0:
raise ValueError("Weight and height must be positive numbers.")
if unit == "imperial":
weight *= 0.453592 # lbs to kg
height *= 0.0254 # inches to meters
bmi = weight / (height ** 2)
category = self.categorize_bmi(bmi)
result_message = f"Your BMI is: {bmi:.2f}\nThis is considered: {category}"
messagebox.showinfo("BMI Result", result_message)
except ValueError as e:
messagebox.showerror("Input Error", str(e))
def categorize_bmi(self, bmi):
if bmi < 18.5:
return "Underweight"
elif 18.5 <= bmi < 24.9:
return "Normal weight"
elif 25 <= bmi < 29.9:
return "Overweight"
else:
return "Obesity"
if __name__ == "__main__":
app = BMICalculator()
app.mainloop()