Building a Simple BMI Calculator with Python and Tkinter

ujjwal Sonkar
4 min readJul 18, 2023

--

Body Mass Index (BMI) is a measure of body fat based on an individual's weight and height. It provides a general indication of whether someone has a healthy body weight or is underweight, overweight, or obese.

In this tutorial, we will walk through the process of building a simple BMI calculator using Python and the Tkinter library. Let's get started!

Introduction

In this tutorial, we will create a graphical user interface (GUI) application that calculates BMI based on user input for age, height, and weight. We will use the Tkinter library, which is a standard Python interface to the Tk GUI toolkit, to develop the user interface.

Prerequisites

To follow along with this tutorial, you will need:

  • Python installed on your machine (version 3 or above)
  • The Tkinter library, which is usually included with Python distributions.

Step 1: Importing Libraries

import tkinter as tk
from tkinter import messagebox

The tkinter library is imported as tk and provides a Python interface to the Tk GUI toolkit, which allows us to create the graphical user interface for our BMI calculator.
The messagebox module from tkinter is imported specifically for displaying message boxes to show the BMI category.

Step 2: Creating the BMI Class and Initializing the GUI

class BMI:
def __init__(self):
self.root = tk.Tk()
self.root.title("BMI Calculator")

self.label = tk.Label(self.root, text="BMI Calculator")
self.label.pack()

# Creating labels and entry fields for age, height, and weight
self.label_age = tk.Label(self.root, text="Age:")
self.label_age.pack()
self.entry_age = tk.Entry(self.root)
self.entry_age.pack()

self.label_height = tk.Label(self.root, text="Height (cm):")
self.label_height.pack()
self.entry_height = tk.Entry(self.root)
self.entry_height.pack()

self.label_weight = tk.Label(self.root, text="Weight (kg):")
self.label_weight.pack()
self.entry_weight = tk.Entry(self.root)
self.entry_weight.pack()

self.button = tk.Button(self.root, text="Calculate", command=self.calculate_bmi)
self.button.pack()

The BMI class is defined to encapsulate the functionality of the BMI calculator.
In the __init__ method, we create the main window using tk.Tk(), set the window title to "BMI Calculator", and create a label with the text "BMI Calculator" using tk.Label.
We then create labels and entry fields for age, height, and weight using tk.Label and tk.Entry respectively. These allow the user to input their information.
Finally, a calculate button is created using tk.Button, with the command set to self.calculate_bmi. This button triggers the BMI calculation when clicked.

Step 3: Implementing the BMI Calculation and Display

class BMI:
# ...

def calculate_bmi(self):
age = int(self.entry_age.get())
height = int(self.entry_height.get())
weight = int(self.entry_weight.get())

# Calculate BMI
height_in_meters = height / 100
bmi = weight / (height_in_meters ** 2)

# Display BMI category
if bmi <= 16:
messagebox.showinfo("BMI Category", "Severe Thinness")
elif 16 < bmi <= 17:
messagebox.showinfo("BMI Category", "Mild Thinness")
elif 17 < bmi <= 18.5:
messagebox.showinfo("BMI Category", "Moderate Thinness")
elif 18.5 < bmi <= 25:
messagebox.showinfo("BMI Category", "Normal")
elif 25 < bmi <= 30:
messagebox.showinfo("BMI Category", "Overweight")
elif 30 <= bmi <= 35:
messagebox.showinfo("BMI Category", "Obese Class I")
elif 35 <= bmi <= 40:
messagebox.showinfo("BMI Category", "Obese Class II")
elif bmi > 40:
messagebox.showinfo("BMI Category", "Obese Class III")


The BMI ranges and categories used in the code are as follows:

BMI <= 16: Severe Thinness
16 < BMI <= 17: Mild Thinness
17 < BMI <= 18.5: Moderate Thinness
18.5 < BMI <= 25: Normal
25 < BMI <= 30: Overweight
30 <= BMI <= 35: Obese Class I
35 <= BMI <= 40: Obese Class II
BMI > 40: Obese Class III

These ranges and categories are commonly used to assess an individual’s weight status based on their BMI value.

The calculate_bmi method is called when the calculate button is clicked.

It retrieves the user input for age, height, and weight from the entry fields using self.entry_age.get(), self.entry_height.get(), and self.entry_weight.get() respectively.

BMI is calculated by dividing weight (in kg) by the square of height (in meters).

The calculated BMI is then used to determine the BMI category based on the provided if-elif-else conditions. Each category is associated with a specific range of BMI values and displayed using messagebox.showinfo.

Step 4: Running the Application

    def run(self):
self.root.mainloop()

game = BMI()
game.run()

The run method is responsible for starting the main event loop of the Tkinter application, which handles user interactions with the GUI.
An instance of the BMI class is created as game, and the run method is called to start the application.

Text entry fields where the user can input their age, height and weight.
The final output accurately describes the result shown to the user, indicating that their BMI falls within normal range.

That's it! You now have a simple BMI calculator application that allows users to input their age, height, and weight, and displays their BMI category.

Conclusion

In this tutorial, we have built a simple BMI calculator using Python and Tkinter. We covered the steps to set up the GUI, implement the BMI calculation logic, and display the BMI category. BMI calculators are useful tools for assessing weight status and promoting healthy lifestyle choices.

Feel free to explore further and enhance the BMI calculator by adding additional features or improving the user interface.

Happy coding!

Published by Ujjwal Sonkar on July 18, 2023.

https://www.linkedin.com/in/ujjwal-sonkar-636543204

--

--