Here’s a Python program that calculates BMI (Body Mass Index) and BMR (Basal Metabolic Rate) using Tkinter for the graphical user interface (GUI).
The program includes input fields for weight, height, age, and gender and provides results when the user clicks a button.
Full Code
import tkinter as tk from tkinter import ttk, messagebox def calculate_bmi(): try: weight = float(weight_entry.get()) height = float(height_entry.get()) bmi = weight / (height ** 2) bmi_result.set(f"BMI: {bmi:.2f}") except ValueError: messagebox.showerror("Input Error", "Please enter valid numeric values for weight and height.") def calculate_bmr(): try: weight = float(weight_entry.get()) height = float(height_entry.get()) * 100 # Convert height to cm age = int(age_entry.get()) gender = gender_var.get() if gender == "Male": bmr = 10 * weight + 6.25 * height - 5 * age + 5 elif gender == "Female": bmr = 10 * weight + 6.25 * height - 5 * age - 161 else: messagebox.showerror("Input Error", "Please select a valid gender.") return bmr_result.set(f"BMR: {bmr:.2f} kcal/day") except ValueError: messagebox.showerror("Input Error", "Please enter valid numeric values for weight, height, and age.") # Main application window root = tk.Tk() root.title("BMI and BMR Calculator") root.geometry("400x400") # Variables to store results bmi_result = tk.StringVar() bmr_result = tk.StringVar() # Input Labels and Entries ttk.Label(root, text="Weight (kg):").pack(pady=5) weight_entry = ttk.Entry(root) weight_entry.pack(pady=5) ttk.Label(root, text="Height (m):").pack(pady=5) height_entry = ttk.Entry(root) height_entry.pack(pady=5) ttk.Label(root, text="Age (years):").pack(pady=5) age_entry = ttk.Entry(root) age_entry.pack(pady=5) ttk.Label(root, text="Gender:").pack(pady=5) gender_var = tk.StringVar() gender_combobox = ttk.Combobox(root, textvariable=gender_var) gender_combobox['values'] = ("Male", "Female") gender_combobox.pack(pady=5) # Buttons for BMI and BMR calculation ttk.Button(root, text="Calculate BMI", command=calculate_bmi).pack(pady=10) ttk.Button(root, text="Calculate BMR", command=calculate_bmr).pack(pady=10) # Results display ttk.Label(root, textvariable=bmi_result, font=("Arial", 12)).pack(pady=10) ttk.Label(root, textvariable=bmr_result, font=("Arial", 12)).pack(pady=10) # Start the Tkinter event loop root.mainloop()
Explanation of the Code
- Imports:
- tkinter: Used to create the GUI components.
- ttk: Provides themed widgets like labels, entries, buttons, and combo boxes.
- messagebox: Used to show error messages for invalid input.
- Function to Calculate BMI:
- calculate_bmi: Takes weight (in kg) and height (in meters) as input, computes BMI using the formula: BMI=weightheight2BMI = \frac{\text{weight}}{\text{height}^2}
- Updates the bmi_result variable to display the BMI.
- Function to Calculate BMR:
- calculate_bmr: Uses the Harris-Benedict formula for BMR:
- Male: BMR=10⋅weight+6.25⋅height−5⋅age+5BMR = 10 \cdot \text{weight} + 6.25 \cdot \text{height} – 5 \cdot \text{age} + 5
- Female: BMR=10⋅weight+6.25⋅height−5⋅age−161BMR = 10 \cdot \text{weight} + 6.25 \cdot \text{height} – 5 \cdot \text{age} – 161
- Converts height to centimeters before calculation.
- Displays the result in bmr_result.
- calculate_bmr: Uses the Harris-Benedict formula for BMR:
- Tkinter GUI Setup:
- Creates an application window (root) with a title and size.
- Adds labels and entry widgets for user inputs: weight, height, age, and gender.
- Uses a ttk.Combobox for gender selection.
- Adds buttons to trigger BMI and BMR calculations.
- Displaying Results:
- bmi_result and bmr_result are StringVar objects bound to labels to dynamically update and display results.
- Error Handling:
- Ensures numeric input for weight, height, and age. Displays error messages using messagebox for invalid inputs.
Features
- Input Validation: Prevents invalid inputs and shows helpful error messages.
- Responsive GUI: Automatically updates results in the display.
- Customizable: Easily adaptable for additional features like saving results or generating reports.
This application provides a beginner-friendly implementation of a BMI and BMR calculator using Tkinter. You can expand it further by adding more features like different units for input (e.g., pounds or inches).