In this tutorial, we will create a simple yet professional-looking BMI (Body Mass Index) Calculator using Tkinter, Python's standard GUI library. This application will allow users to input their weight and height, calculate their BMI, and display the corresponding health category.
Prerequisites
To follow along with this tutorial, you need:
-
Basic knowledge of Python
-
Python installed on your system (Python 3.x recommended)
-
Tkinter (comes pre-installed with Python)
Step 1: Import Required Modules
We start by importing the necessary modules:
from tkinter import *
from tkinter import ttk
-
Tkinter
is used to create the graphical user interface. -
ttk
provides additional styling features.
Step 2: Define colors
To maintain a professional and visually appealing interface, we define a color palette:
co0 = "#2F3C58" # Dark blue-gray for text
co1 = "#FFFFFF" # White for background
co2 = "#4CAF50" # Green for buttons (professional and friendly)
co3 = "#F4F6F9" # Light gray background for the window
co4 = "#007BFF" # Blue for headers and results
Step 3: Create the main window
We initialize the main application window:
window = Tk()
window.title('BMI Calculator')
window.geometry('410x400')
window.configure(bg=co3)
-
Tk()
creates the main window. -
title()
sets the window title. -
geometry()
sets the window size. -
configure(bg=co3)
applies the background color.
Step 4: Create frames
We structure the layout using two frames:
frame_top = Frame(window, width=350, height=60, bg=co4, pady=10, padx=10, relief="flat")
frame_top.grid(row=0, column=0, sticky=NSEW)
frame_bottom = Frame(window, width=350, height=300, bg=co3, pady=20, padx=20, relief="flat")
frame_bottom.grid(row=1, column=0, sticky=NSEW)
Step 5: Configure the title
We add a title label in the top frame:
app_name = Label(frame_top, text="BMI Calculator", width=23, height=1, font=('Helvetica 16 bold'), bg=co4, fg=co1)
app_name.place(x=0, y=15)
Step 6: Define the BMI calculation function
We create the function to calculate BMI based on user input:
def calculate():
weight = float(e_weight.get())
height = float(e_height.get()) ** 2
result = weight / height
if result < 18.6:
l_result_text['text'] = "Your BMI is: Underweight"
elif result >= 18.5 and result < 24.9:
l_result_text['text'] = "Your BMI is: Normal"
elif result >= 25 and result < 29.9:
l_result_text['text'] = "Your BMI is: Overweight"
else:
l_result_text['text'] = "Your BMI is: Obesity"
l_result['text'] = "{:.2f}".format(result)
Step 7: Create input fields for weight and height
We add entry fields for user input:
l_weight = Label(frame_bottom, text="Enter your weight (kg)", font=('Helvetica 12'), bg=co3, fg=co0)
l_weight.grid(row=0, column=0, pady=10, padx=10, sticky=NW)
e_weight = Entry(frame_bottom, width=15, font=('Helvetica 12'), justify='center', relief=SOLID)
e_weight.grid(row=0, column=1, pady=10, padx=10, sticky=NSEW)
l_height = Label(frame_bottom, text="Enter your height (m)", font=('Helvetica 12'), bg=co3, fg=co0)
l_height.grid(row=1, column=0, pady=10, padx=10, sticky=NW)
e_height = Entry(frame_bottom, width=15, font=('Helvetica 12'), justify='center', relief=SOLID)
e_height.grid(row=1, column=1, pady=10, padx=10, sticky=NSEW)
Step 8: Display the BMI result
We create labels to display the BMI score and category:
l_result = Label(frame_bottom, width=5, text="---", font=('Helvetica 24 bold'), bg=co2, fg=co1)
l_result.grid(row=2, column=0, columnspan=2, pady=20, padx=10)
l_result_text = Label(frame_bottom, width=37, text="", font=('Helvetica 12'), bg=co3, fg=co0)
l_result_text.grid(row=3, column=0, columnspan=2, pady=10, padx=10)
Step 9: Add a calculate button
We add a button to trigger the BMI calculation:
b_calculate = Button(frame_bottom, command=calculate, text="Calculate", width=34, font=('Helvetica 12 bold'), bg=co2, fg="white", relief=RAISED, bd=2)
b_calculate.grid(row=4, column=0, columnspan=2, pady=20, padx=10)
Step 10: Run the application
Finally, we start the Tkinter main loop:
window.mainloop()
Conclusion
In this tutorial, we created a fully functional BMI Calculator using Tkinter. We structured the application using frames, styled it with colors, and implemented BMI calculations with appropriate health categories. You can enhance this app further by adding more features such as unit conversion or graphical visualization. Happy coding!
Copyright Statement: Unless stated otherwise, all articles are original to this site, please credit the source when sharing.
Article link:http://pybeginners.com/python-projects/how-to-build-a-bmi-calculator-in-python-tkinter-gui-project-for-beginners/
License Agreement:Attribution-NonCommercial 4.0 International License