Python Forum
[Tkinter] Reset Button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Reset Button
#1
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()
Reply
#2
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()
Reply
#3
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()
Reply
#4
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I add reset button? andrex353 2 3,054 Jun-12-2022, 04:41 PM
Last Post: deanhystad
  Reset Button did not work ardvci 2 2,723 Mar-02-2020, 07:59 PM
Last Post: ardvci
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,949 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020