In [ ]:
import tkinter as tk
from tkinter import messagebox
In [ ]:
# Function to calculate BMI and show results
def calculate_bmi():
try:
# Get the height and weight input from the user
height = float(entry_height.get())
weight = float(entry_weight.get())
# Convert height from cm to meters
height = height / 100
# Calculate BMI
bmi = weight / (height * height)
# Show BMI result
label_result.config(text=f"Your BMI: {bmi:.2f}")
# Display BMI category based on the value
if bmi <= 16:
category = "Severely Underweight"
elif bmi <= 18.5:
category = "Underweight"
elif bmi <= 25:
category = "Healthy"
elif bmi <= 30:
category = "Overweight"
else:
category = "Severely Overweight"
label_category.config(text=f"Category: {category}")
except ValueError:
# Show error message if the input is invalid
[Link]("Input Error", "Please enter valid numerical values for hei
ght and weight.")
# Set up the main window
root = [Link]()
[Link]("BMI Calculator")
# Create labels and entry widgets for height and weight
label_height = [Link](root, text="Enter your height in cm:")
label_height.grid(row=0, column=0, padx=10, pady=10)
entry_height = [Link](root)
entry_height.grid(row=0, column=1, padx=10, pady=10)
label_weight = [Link](root, text="Enter your weight in kg:")
label_weight.grid(row=1, column=0, padx=10, pady=10)
entry_weight = [Link](root)
entry_weight.grid(row=1, column=1, padx=10, pady=10)
# Create button to calculate BMI
btn_calculate = [Link](root, text="Calculate BMI", command=calculate_bmi)
btn_calculate.grid(row=2, column=0, columnspan=2, pady=20)
# Create labels to display results
label_result = [Link](root, text="Your BMI: ")
label_result.grid(row=3, column=0, columnspan=2)
label_category = [Link](root, text="Category: ")
label_category.grid(row=4, column=0, columnspan=2)
# Start the GUI event loop
[Link]()
Pranjal Gajbhiye(AIE)
Happy Learning..
In [ ]: