|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +Python Desktop application using Tkinter to calculate BMI |
| 6 | +Author: Reshma |
| 7 | +""" |
| 8 | + |
| 9 | +# importing tkinter |
| 10 | +import tkinter as tk |
| 11 | + |
| 12 | +class BmiCalculator: |
| 13 | + def __init__(self): |
| 14 | + self.root = tk.Tk() |
| 15 | + # Dimension of the application |
| 16 | + self.root.geometry("400x530") |
| 17 | + # Title of the desktop app |
| 18 | + self.root.title("BMI Calculator v.1") |
| 19 | + # Background color of the application root |
| 20 | + self.root.config(bg="#ccffff") |
| 21 | + #variables |
| 22 | + self.weight_var = tk.StringVar() |
| 23 | + self.height_var = tk.StringVar() |
| 24 | + # label |
| 25 | + self.label1 = tk.Label(self.root,text="Enter your height in cm",font=("Arial",16),bg="#ccffff") |
| 26 | + self.label1.pack(pady=27) |
| 27 | + # Getting height using Entry |
| 28 | + self.heightEntry = tk.Entry(self.root,textvariable=self.height_var,width=15,font=("Arial",16),bd=3) |
| 29 | + self.heightEntry.pack(pady=10) |
| 30 | + # label |
| 31 | + self.label2 = tk.Label(self.root, text="Enter your weight in kg", font=("Arial", 16),bg="#ccffff") |
| 32 | + self.label2.pack(pady=10) |
| 33 | + # Getting weight using Entry |
| 34 | + self.weightEntry = tk.Entry(self.root,textvariable=self.weight_var,width=15, font=("Arial", 16),bd=3) |
| 35 | + self.weightEntry.pack(pady=20) |
| 36 | + # Button to calculate BMI |
| 37 | + self.button = tk.Button(self.root,text="Calculate BMI",font=("Times New Roman",16),bg="black",fg="white",command=self.findBMI) |
| 38 | + self.button.pack(padx=30,pady=20) |
| 39 | + # label to display the calculated BMI |
| 40 | + self.bmiLabel = tk.Label(self.root,font=("Tahoma",15),bg="#ccffff") |
| 41 | + self.bmiLabel.pack(pady=7) |
| 42 | + # label to display BMI result |
| 43 | + self.result = tk.Label(self.root,font=("Tahoma",17),text="",height=2,width=36,bg="#ccffff") |
| 44 | + self.result.pack(padx=30,pady=5) |
| 45 | + self.root.mainloop() |
| 46 | + |
| 47 | + # Function to calculate BMI based on inputted height and weight |
| 48 | + def findBMI(self): |
| 49 | + weight = self.weight_var.get() |
| 50 | + height = self.height_var.get() |
| 51 | + # When the height and weight are empty or filled with space |
| 52 | + if weight=="" or weight.isspace() or height=="" or height.isspace(): |
| 53 | + self.result.config(text="Height and Weight cannot be empty",bg="brown",fg="white",font=("Tahoma",15)) |
| 54 | + # when height and weight are input properly as numbers |
| 55 | + elif weight.isdecimal() and height.isdecimal(): |
| 56 | + # converted height in centimetre to metre |
| 57 | + heightMetre = float(height)/100 |
| 58 | + # calculating bmi |
| 59 | + bmi = float("{:.2f}".format(float(weight)/heightMetre/heightMetre)) |
| 60 | + # Displaying bmi through label 'bmiLabel' |
| 61 | + self.bmiLabel.config(text=f"BMI is {bmi}") |
| 62 | + # Category of BMI result |
| 63 | + if bmi<15: |
| 64 | + self.result.config(text="Very severely underweight",bg="purple",fg="white",font=("Georgia")) |
| 65 | + elif bmi>=15 and bmi<16: |
| 66 | + self.result.config(text="Severely underweight", bg="blue", fg="white",font=("Georgia")) |
| 67 | + elif bmi>=16 and bmi<18.5: |
| 68 | + self.result.config(text="Underweight", bg="#3377ff", fg="black",font=("Georgia",18)) |
| 69 | + elif bmi>=18.5 and bmi<=24.9: |
| 70 | + self.result.config(text="Healthy", bg="green", fg="white",font=("Georgia",20)) |
| 71 | + elif bmi>=25 and bmi<=29.9: |
| 72 | + self.result.config(text="Overweight", bg="yellow", fg="black",font=("Georgia",20)) |
| 73 | + elif bmi>=30 and bmi<40: |
| 74 | + self.result.config(text="Obese", bg="orange", fg="black",font=("Georgia",20)) |
| 75 | + elif bmi>=40: |
| 76 | + self.result.config(text="Extremely Obese", bg="red", fg="white",font=("Cambria",23)) |
| 77 | + else: |
| 78 | + # When the entered input in height and weight fiels is not numbers |
| 79 | + self.result.config(text="Enter numbers only please",bg="red",font=("Arial",18)) |
| 80 | +# object created for the class 'BmiCalculator' |
| 81 | +BmiCalculator() |
0 commit comments