Question: Python Programming: Hi! I am writing a GUI in Python that inputs the users name, weight, and height and returns their name, bmi calculation, and
Python Programming:
Hi! I am writing a GUI in Python that inputs the users name, weight, and height and returns their name, bmi calculation, and bmi category (ex:Underweight). However, I am running into a few problems with my program.
Here's what I have so far:
from tkinter import* master = Tk() master.title("BMI Calculator") status = StringVar() Label(master, text="Name").grid(row=0, column=0) Label(master, text="Height (Inches)").grid(row=3, column=0) Label(master, text="Weight (Pounds)").grid(row=3, column=1) l1 = Label(master, textvariable=status).grid(row=9, column=0) name = Entry(master) h = Entry(master) w = Entry(master)
name.grid(row=2, column=0, padx=10, pady=10) h.grid(row=4, column=0, padx=10, pady=10) w.grid(row=4, column=1, padx=10, pady=10) Button(master, text='Calculate BMI', command= lambda: BMI(h, w).grid(row=8, column=1, sticky=W, pady=4)
def user_name(name): n = str(name.get()) status.set(n)
def BMI(h, w): inch_height = float(h.get()) weight = float(w.get()) meter_height = inch_height / 39.37 kilo_weight = weight * 0.453592 bmi = kilo_weight / (meter_height ** 2) return status.set(bmi) def BMI_interpretation(h, w): inch_height = float(h.get()) weight = float(w.get()) meter_height = inch_height / 39.37 kilo_weight = weight * 0.453592 bmi = kilo_weight / (meter_height ** 2) if bmi <= 18.5: status.set("Underweight") elif bmi > 18.5 and bmi <= 24.9: status.set("Normal") elif bmi > 24.9 and bmi <= 29.9: status.set("Overweight") else: status.set("Obese") master.mainloop()
However, Python is saying that my functions are invalid syntax (there is a red x symbol by def user_name(name), and when i remove the function completely, the red x moves to the next function). I am not sure why these functions are not working in my program. Also, I need the button in Tkinter to return all of values of the functions (name, BMI, and BMI category), not just the BMI(h, w) function.
Thanks!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
