Posts: 2
Threads: 1
Joined: Jan 2019
Hi, I've decided to make a BMI calculator using Tkinter. I have everything I need so far except the reset button. I have been using a text variable in labelYourBMI2. It comes up blank. My Problem I think is around line 70. Thanks. PS I just started using Python so it's a mess
#Imports tkinter to create a window.
from tkinter import *
root=Tk()
root.title("BMI Calculator By")
#Creates Frames for items to go in.
topFrame=Frame(root)
topFrame.pack()
bottomFrame=Frame(root)
bottomFrame.pack(side=BOTTOM)
#Creates labels to display how the app works.
theLabel = Label(topFrame, text="Body Mass Index also called BMI is used in the health industry")
theLabel.pack()
theLabel2 = Label(topFrame, text="to measure how over or underweight you are.")
theLabel2.pack()
theLabel3 = Label(topFrame, text="An ideal BMI ranges between 18.5 and 24.9.")
theLabel3.pack()
theLabel4 = Label(topFrame, text="bmi = weight /(height^2)*703")
theLabel4.pack()
theLabel5 = Label(topFrame, text="This is an example of someone who used our app.")
labelExamplename = Label(topFrame, text="Name = Boi")
labelExamplename.pack()
labelExampleheight = Label(topFrame, text="Height(in) = 69")
labelExampleheight.pack()
labelExampleweight = Label(topFrame, text="Weight(lb) = 160")
labelExampleweight.pack()
#Creates and displays variables to further display how it works.
name = "Boi"
height_in = 69
weight_lb = 160
bmi = weight_lb / (height_in ** 2)*703
labelExamplelabel = Label(topFrame, text="Boi's BMI is:")
labelExamplelabel.pack()
labelExample = Label(topFrame, text=bmi)
labelExample.pack()
#Entry Boxes and Labels for Height
LabelWhatisyourHeight = Label(topFrame, text="What is your height in inches?")
LabelWhatisyourHeight.pack()
Height = StringVar()
EntryBoxHeight = Entry(topFrame, textvariable=Height)
EntryBoxHeight.pack()
LabelWhatisyourHeightLabel = Label(topFrame, text="inches")
LabelWhatisyourHeightLabel.pack()
#Entry Boxes and Labels for Weight
LabelWhatisYourWeight = Label(topFrame, text="What is you Weight in pounds?")
LabelWhatisYourWeight.pack()
Weight = StringVar()
EntryBoxWeight = Entry(topFrame, textvariable=Weight)
EntryBoxWeight.pack()
LabelWhatisYourWeightLabel = Label(topFrame, text="pounds")
LabelWhatisYourWeightLabel.pack()
#Creates A Space
LabelNothing = Label(topFrame, text = "")
#Calculation Button (Figure out how to link this to a function
def calculate():
pounds = float(Weight.get())
inches = float(Height.get())
labelYourBMI = Label(topFrame, text="Your BMI is")
labelYourBMI.pack()
bmitwo = (pounds) / (inches)**2*703
labelYourBMI2 = Label(topFrame, textvariable=bmitwo)
labelYourBMI2.pack()
return
ButtonCalculate = Button(topFrame, text="Calculate", command=calculate)
ButtonCalculate.pack()
#make sure to define the reset button
def reset():
labelYourBMI2 = Button(topFrame, text="")
return
ButtonReset = Button(topFrame, text="Reset", command=reset)
ButtonReset.pack()
root.mainloop()
Posts: 12,030
Threads: 485
Joined: Sep 2016
Try this:
#Imports tkinter to create a window.
from tkinter import *
root=Tk()
root.title("BMI Calculator By")
#Creates Frames for items to go in.
topFrame=Frame(root)
topFrame.pack()
bottomFrame=Frame(root)
bottomFrame.pack(side=BOTTOM)
#Creates labels to display how the app works.
theLabel = Label(topFrame, text="Body Mass Index also called BMI is used in the health industry")
theLabel.pack()
theLabel2 = Label(topFrame, text="to measure how over or underweight you are.")
theLabel2.pack()
theLabel3 = Label(topFrame, text="An ideal BMI ranges between 18.5 and 24.9.")
theLabel3.pack()
theLabel4 = Label(topFrame, text="bmi = weight /(height^2)*703")
theLabel4.pack()
theLabel5 = Label(topFrame, text="This is an example of someone who used our app.")
labelExamplename = Label(topFrame, text="Name = Boi")
labelExamplename.pack()
labelExampleheight = Label(topFrame, text="Height(in) = 69")
labelExampleheight.pack()
labelExampleweight = Label(topFrame, text="Weight(lb) = 160")
labelExampleweight.pack()
#Creates and displays variables to further display how it works.
name = "Boi"
height_in = 69
weight_lb = 160
bmi = weight_lb / (height_in ** 2)*703
labelExamplelabel = Label(topFrame, text="Boi's BMI is:")
labelExamplelabel.pack()
labelExample = Label(topFrame, text=bmi)
labelExample.pack()
#Entry Boxes and Labels for Height
LabelWhatisyourHeight = Label(topFrame, text="What is your height in inches?")
LabelWhatisyourHeight.pack()
Height = StringVar()
EntryBoxHeight = Entry(topFrame, textvariable=Height)
EntryBoxHeight.pack()
LabelWhatisyourHeightLabel = Label(topFrame, text="inches")
LabelWhatisyourHeightLabel.pack()
#Entry Boxes and Labels for Weight
LabelWhatisYourWeight = Label(topFrame, text="What is you Weight in pounds?")
LabelWhatisYourWeight.pack()
Weight = StringVar()
EntryBoxWeight = Entry(topFrame, textvariable=Weight)
EntryBoxWeight.pack()
LabelWhatisYourWeightLabel = Label(topFrame, text="pounds")
LabelWhatisYourWeightLabel.pack()
#Creates A Space
LabelNothing = Label(topFrame, text = "")
#Calculation Button (Figure out how to link this to a function
bmi = StringVar()
bmi.set("")
def calculate():
pounds = float(Weight.get())
inches = float(Height.get())
# labelYourBMI = Label(topFrame, text="Your BMI is")
bmi.set("Your BMI is")
labelYourBMI = Label(topFrame, textvariable=bmi)
labelYourBMI.pack()
bmitwo = (pounds) / (inches)**2*703
labelYourBMI2 = Label(topFrame, textvariable=bmitwo)
labelYourBMI2.pack()
return
ButtonCalculate = Button(topFrame, text="Calculate", command=calculate)
ButtonCalculate.pack()
#make sure to define the reset button
def reset():
# labelYourBMI2 = Button(topFrame, text="")
bmi.set("")
Height.set('')
Weight.set('')
return
ButtonReset = Button(topFrame, text="Reset", command=reset)
ButtonReset.pack()
root.mainloop()
Posts: 2
Threads: 1
Joined: Jan 2019
Jan-25-2019, 10:49 PM
(This post was last modified: Jan-25-2019, 10:49 PM by CPD3408.)
That got me closer, it reset all of the entries but did not reset the actual labelYourBMI2 label. I tried setting BMITWO to ("") But it just came back as an error. Also I changed the Text Variables to just regular text because when I hit the calculate button nothing showed up in the labelYourBMI2 and it did with just regular text.
#Imports tkinter to create a window.
from tkinter import *
root=Tk()
root.title("BMI Calculator By")
#Creates Frames for items to go in.
topFrame=Frame(root)
topFrame.pack()
bottomFrame=Frame(root)
bottomFrame.pack(side=BOTTOM)
#Creates labels to display how the app works.
theLabel = Label(topFrame, text="Body Mass Index also called BMI is used in the health industry")
theLabel.pack()
theLabel2 = Label(topFrame, text="to measure how over or underweight you are.")
theLabel2.pack()
theLabel3 = Label(topFrame, text="An ideal BMI ranges between 18.5 and 24.9.")
theLabel3.pack()
theLabel4 = Label(topFrame, text="bmi = weight /(height^2)*703")
theLabel4.pack()
theLabel5 = Label(topFrame, text="This is an example of someone who used our app.")
labelExamplename = Label(topFrame, text="Name = Boi")
labelExamplename.pack()
labelExampleheight = Label(topFrame, text="Height(in) = 69")
labelExampleheight.pack()
labelExampleweight = Label(topFrame, text="Weight(lb) = 160")
labelExampleweight.pack()
#Creates and displays variables to further display how it works.
name = "Boi"
height_in = 69
weight_lb = 160
bmi = weight_lb / (height_in ** 2)*703
labelExamplelabel = Label(topFrame, text="Boi's BMI is:")
labelExamplelabel.pack()
labelExample = Label(topFrame, text=bmi)
labelExample.pack()
#Entry Boxes and Labels for Height
LabelWhatisyourHeight = Label(topFrame, text="What is your height in inches?")
LabelWhatisyourHeight.pack()
Height = StringVar()
EntryBoxHeight = Entry(topFrame, textvariable=Height)
EntryBoxHeight.pack()
LabelWhatisyourHeightLabel = Label(topFrame, text="inches")
LabelWhatisyourHeightLabel.pack()
#Entry Boxes and Labels for Weight
LabelWhatisYourWeight = Label(topFrame, text="What is you Weight in pounds?")
LabelWhatisYourWeight.pack()
Weight = StringVar()
EntryBoxWeight = Entry(topFrame, textvariable=Weight)
EntryBoxWeight.pack()
LabelWhatisYourWeightLabel = Label(topFrame, text="pounds")
LabelWhatisYourWeightLabel.pack()
#Creates A Space
LabelNothing = Label(topFrame, text = "")
# Calculation Button (Figure out how to link this to a function
bmi = StringVar()
bmi.set("")
#Calculation Button (Figure out how to link this to a function
def calculate():
pounds = float(Weight.get())
inches = float(Height.get())
# labelYourBMI = Label(topFrame, text="Your BMI is")
bmi.set("Your BMI is")
labelYourBMI = Label(topFrame, text=bmi)
labelYourBMI.pack()
bmitwo = (pounds) / (inches) ** 2 * 703
labelYourBMI2 = Label(topFrame, text=bmitwo)
labelYourBMI2.pack()
return
ButtonCalculate = Button(topFrame, text="Calculate", command=calculate)
ButtonCalculate.pack()
#make sure to define the reset button
def reset():
# labelYourBMI2 = Button(topFrame, text="")
bmi.set("")
Height.set('')
Weight.set('')
return
ButtonReset = Button(topFrame, text="Reset", command=reset)
ButtonReset.pack()
root.mainloop()
Posts: 12,030
Threads: 485
Joined: Sep 2016
so do the same for labelYourBMI2 as done for labelYourBMI
This should have been obvious, make an effort!
#Imports tkinter to create a window.
from tkinter import *
root=Tk()
root.title("BMI Calculator By")
#Creates Frames for items to go in.
topFrame=Frame(root)
topFrame.pack()
bottomFrame=Frame(root)
bottomFrame.pack(side=BOTTOM)
#Creates labels to display how the app works.
theLabel = Label(topFrame, text="Body Mass Index also called BMI is used in the health industry")
theLabel.pack()
theLabel2 = Label(topFrame, text="to measure how over or underweight you are.")
theLabel2.pack()
theLabel3 = Label(topFrame, text="An ideal BMI ranges between 18.5 and 24.9.")
theLabel3.pack()
theLabel4 = Label(topFrame, text="bmi = weight /(height^2)*703")
theLabel4.pack()
theLabel5 = Label(topFrame, text="This is an example of someone who used our app.")
labelExamplename = Label(topFrame, text="Name = Boi")
labelExamplename.pack()
labelExampleheight = Label(topFrame, text="Height(in) = 69")
labelExampleheight.pack()
labelExampleweight = Label(topFrame, text="Weight(lb) = 160")
labelExampleweight.pack()
#Creates and displays variables to further display how it works.
name = "Boi"
height_in = 69
weight_lb = 160
bmi = weight_lb / (height_in ** 2)*703
labelExamplelabel = Label(topFrame, text="Boi's BMI is:")
labelExamplelabel.pack()
labelExample = Label(topFrame, text=bmi)
labelExample.pack()
#Entry Boxes and Labels for Height
LabelWhatisyourHeight = Label(topFrame, text="What is your height in inches?")
LabelWhatisyourHeight.pack()
Height = StringVar()
EntryBoxHeight = Entry(topFrame, textvariable=Height)
EntryBoxHeight.pack()
LabelWhatisyourHeightLabel = Label(topFrame, text="inches")
LabelWhatisyourHeightLabel.pack()
#Entry Boxes and Labels for Weight
LabelWhatisYourWeight = Label(topFrame, text="What is you Weight in pounds?")
LabelWhatisYourWeight.pack()
Weight = StringVar()
EntryBoxWeight = Entry(topFrame, textvariable=Weight)
EntryBoxWeight.pack()
LabelWhatisYourWeightLabel = Label(topFrame, text="pounds")
LabelWhatisYourWeightLabel.pack()
#Creates A Space
LabelNothing = Label(topFrame, text = "")
#Calculation Button (Figure out how to link this to a function
bmi = StringVar()
bmi.set("")
bmi2 = StringVar()
bmi2.set("")
def calculate():
pounds = float(Weight.get())
inches = float(Height.get())
# labelYourBMI = Label(topFrame, text="Your BMI is")
bmi.set("Your BMI is")
labelYourBMI = Label(topFrame, textvariable=bmi)
labelYourBMI.pack()
bmi2.set((pounds) / (inches)**2*703)
# bmitwo = (pounds) / (inches)**2*703
labelYourBMI2 = Label(topFrame, textvariable=bmi2)
labelYourBMI2.pack()
return
ButtonCalculate = Button(topFrame, text="Calculate", command=calculate)
ButtonCalculate.pack()
#make sure to define the reset button
def reset():
# labelYourBMI2 = Button(topFrame, text="")
bmi.set("")
bmi2.set("")
Height.set('')
Weight.set('')
return
ButtonReset = Button(topFrame, text="Reset", command=reset)
ButtonReset.pack()
root.mainloop()
|