In this tutorial, weβll build BodyMetrics Pro v2.0, a desktop BMI Risk Prediction System using:
π Python
π₯οΈ Tkinter + ttkbootstrap (modern UI)
π€ Scikit-Learn (Logistic Regression)
π Matplotlib (visual BMI chart)
By the end, youβll have a fully working AI-powered health app.
π Project Repository
Clone the full project here:
https://github.com/rogers-cyber/python-tiny-tools/tree/main/BMI-prediction-system
1οΈβ£ What This App Does
The app:
Calculates BMI
Uses Machine Learning to predict health risk
Considers Age and Gender
Displays results visually with a BMI chart
Risk levels:
β Low Risk
β οΈ Moderate Risk
β High Risk
2οΈβ£ Install Required Libraries
Before coding, install dependencies:
pip install numpy scikit-learn matplotlib ttkbootstrap
3οΈβ£ Import Required Modules
We start by importing all necessary libraries.
import tkinter as tk
from tkinter import messagebox
import ttkbootstrap as tb
from ttkbootstrap.constants import *
Why?
tkinter β GUI framework
ttkbootstrap β modern UI themes
messagebox β error alerts
Machine Learning & Math Imports
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
numpy β numerical operations
LogisticRegression β ML classifier
StandardScaler β feature normalization
Charting Library
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
This lets us embed charts inside Tkinter.
4οΈβ£ Building the Machine Learning Model
We encapsulate ML logic inside a class.
class BMIRiskModel:
def __init__(self):
self.scaler = StandardScaler()
self.model = LogisticRegression()
self._train()
Why a class?
Keeps ML logic clean
Easy to reuse and maintain
Training the Model
def _train(self):
X = np.array([
[18, 18, 0], [22, 25, 1], [24, 30, 0],
[27, 40, 1], [29, 45, 0], [32, 50, 1],
[35, 55, 0], [38, 60, 1], [42, 65, 0]
])
Each row contains:
[BMI, Age, Gender]
Gender:
Male = 1
Female = 0
Risk Labels
y = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])
0 β Low Risk
1 β Moderate Risk
2 β High Risk
Scaling & Training
X_scaled = self.scaler.fit_transform(X)
self.model.fit(X_scaled, y)
Scaling ensures fair ML predictions.
Making Predictions
def predict(self, bmi, age, gender):
X = np.array([[bmi, age, gender]])
X_scaled = self.scaler.transform(X)
return self.model.predict(X_scaled)[0]
5οΈβ£ Utility Functions
BMI Calculation
def calculate_bmi(weight, height):
return round(weight / (height ** 2), 2)
Risk Label Mapping
def risk_label(code):
return {
0: ("Low Risk", "β
"),
1: ("Moderate Risk", "β οΈ"),
2: ("High Risk", "β")
}[code]
6οΈβ£ Creating the Main Application Window
class BMIPredictorApp:
APP_NAME = "BodyMetrics Pro"
APP_VERSION = "2.0"
Initialize the App
def __init__(self):
self.root = tb.Window(themename="darkly")
self.root.title("BodyMetrics Pro v2.0")
self.root.minsize(720, 620)
self.model = BMIRiskModel()
self._build_ui()
7οΈβ£ Building the User Interface
App Title
tb.Label(
main,
text="π§ BodyMetrics Pro",
font=("Segoe UI", 22, "bold")
).pack()
Input Fields
self.weight = tb.Entry(form)
self.height = tb.Entry(form)
self.age = tb.Entry(form)
Each input collects user data.
Gender Selection
self.gender = tk.StringVar(value="Male")
tb.Combobox(
form,
values=["Male", "Female"],
textvariable=self.gender
)
8οΈβ£ Analyze Button & Progress Bar
tb.Button(
main,
text="π Analyze Health Risk",
bootstyle=SUCCESS,
command=self.analyze
).pack()
self.progress = tb.Progressbar(
main,
maximum=100
)
Gives visual feedback during analysis.
9οΈβ£ BMI Chart Visualization
self.figure = Figure(figsize=(5, 3))
self.ax = self.figure.add_subplot(111)
Updating the Chart
def _update_chart(self, bmi):
self.ax.clear()
self.ax.bar(["Your BMI"], [bmi])
self.ax.axhline(18.5, linestyle="--")
self.ax.axhline(25, linestyle="--")
self.ax.axhline(30, linestyle="--")
These lines show BMI classification zones.
π Processing User Input
def analyze(self):
w = float(self.weight.get())
h = float(self.height.get())
age = int(self.age.get())
Predicting Risk
bmi = calculate_bmi(w, h)
risk_code = self.model.predict(bmi, age, gender)
label, icon = risk_label(risk_code)
Display Result
self.result.config(
text=f"{icon} BMI: {bmi} | {label}",
font=("Segoe UI", 16, "bold")
)
π Running the App
if __name__ == "__main__":
app = BMIPredictorApp()
app.run()
π Final Result
You now have:
β A modern desktop GUI
π€ An ML-based BMI risk predictor
π Interactive BMI visualization
π§© Clean, modular Python code

Top comments (0)