BMI Calculator using Python Tkinter

 BMI Calculator using Python Tkinter

The formula is BMI = kg/m2 where kg is a person’s weight in kilograms and m2 is their height in meters squared. A BMI of 25.0 or more is overweight, while the healthy range is 18.5 to 24.9.

BMI Categories:

1) Underweight = <18.5

2) Normal weight = 18.5–24.9

3) Overweight = 25–29.9

4) Obesity = BMI of 30 or greater

Functions Explanation:

There can many ways to create an interface for this program but all the programs will have the same function. In this section, we will see how the main function works in this program. So there are two functions used to display the BMI result.

1) calculate_bmi()

2) bmi_index()

Source Code:

Here is the complete source code of the BMI calculator. The code can be run on any version of python3 but it must have Tkinter installed on it. In case of any doubt or error please leave a comment.


from tkinter import *

from tkinter import messagebox


def reset_entry():

    age_tf.delete(0,'end')

    height_tf.delete(0,'end')

    weight_tf.delete(0,'end')


def calculate_bmi():

    kg = int(weight_tf.get())

    m = int(height_tf.get())/100

    bmi = kg/(m*m)

    bmi = round(bmi, 1)

    bmi_index(bmi)


def bmi_index(bmi):

    

    if bmi < 18.5:

        messagebox.showinfo('bmi-pythonguides', f'BMI = {bmi} is Underweight')

    elif (bmi > 18.5) and (bmi < 24.9):

        messagebox.showinfo('bmi-pythonguides', f'BMI = {bmi} is Normal')

    elif (bmi > 24.9) and (bmi < 29.9):

        messagebox.showinfo('bmi-pythonguides', f'BMI = {bmi} is Overweight')

    elif (bmi > 29.9):

        messagebox.showinfo('bmi-pythonguides', f'BMI = {bmi} is Obesity') 

    else:

        messagebox.showerror('bmi-pythonguides', 'something went wrong!')   


ws = Tk()

ws.title('PythonGuides')

ws.geometry('400x300')

ws.config(bg='#686e70')


var = IntVar()


frame = Frame(

    ws,

    padx=10, 

    pady=10

)

frame.pack(expand=True)



age_lb = Label(

    frame,

    text="Enter Age (2 - 120)"

)

age_lb.grid(row=1, column=1)


age_tf = Entry(

    frame, 

)

age_tf.grid(row=1, column=2, pady=5)


gen_lb = Label(

    frame,

    text='Select Gender'

)

gen_lb.grid(row=2, column=1)


frame2 = Frame(

    frame

)

frame2.grid(row=2, column=2, pady=5)


male_rb = Radiobutton(

    frame2,

    text = 'Male',

    variable = var,

    value = 1

)

male_rb.pack(side=LEFT)


female_rb = Radiobutton(

    frame2,

    text = 'Female',

    variable = var,

    value = 2

)

female_rb.pack(side=RIGHT)


height_lb = Label(

    frame,

    text="Enter Height (cm)  "

)

height_lb.grid(row=3, column=1)


weight_lb = Label(

    frame,

    text="Enter Weight (kg)  ",


)

weight_lb.grid(row=4, column=1)


height_tf = Entry(

    frame,

)

height_tf.grid(row=3, column=2, pady=5)


weight_tf = Entry(

    frame,

)

weight_tf.grid(row=4, column=2, pady=5)


frame3 = Frame(

    frame

)

frame3.grid(row=5, columnspan=3, pady=10)


cal_btn = Button(

    frame3,

    text='Calculate',

    command=calculate_bmi

)

cal_btn.pack(side=LEFT)


reset_btn = Button(

    frame3,

    text='Reset',

    command=reset_entry

)

reset_btn.pack(side=LEFT)


exit_btn = Button(

    frame3,

    text='Exit',

    command=lambda:ws.destroy()

)

exit_btn.pack(side=RIGHT)


ws.mainloop()


Output:

In this output, the user needs to fill in some information like age, gender, height & weight. Out of this information height & weight is used to calculate the BMI. Then the BMI passes through conditions.

Each condition has a remark (underweight, Normal, overweight, etc). The result is displayed using a message box.





















No comments

Powered by Blogger.