-1

i have made this simple program that calculates how much fuel a plane needs but the problem is that lets say its out of range it displays a out of range label but if you calculate the fuel again it simply calculates it displays it bur continues displaying out of range right below.....i've tried a few things to make the label disappear but they don't work... and vice versa if the fuel required passes 20800 kg it will display out of fuel but also any previous calculations

    #fuel Calculator v1.0
#this version only calculates fuel for a 737 or a a320
from tkinter import *

top = Tk()
top.title("fuel planner")

def boeing_burn():

    res1=(float(E1.get()))* 2530
    res2=round(res1*2.204,2)

    lal1 = Label(top, text = " kg")
    lal2 = Label(top, text = " lbs")
    lal5 = Label(top, text = "out of range")

    lal1["text"]= str(res1) + "kg"
    lal2["text"]=str(res2)+"lbs"


    if res1 < 20800: 
        lal1.grid(column=0,row=2)
        lal2.grid(column=2,row=2)
        lal5.grid_remove()
    elif res1 > 20800:
        lal1.grid_remove()
        lal2.grid_remove()
        lal5.grid(column=1,row=2)

def a320_burn():


    res1=(float(E1.get()))*2430
    res2=round(res1*2.204,2)

    lal1 = Label(top, text = " kg")
    lal2 = Label(top, text = " lbs")
    lal5 = Label(top, text = "out of range")

    lal1["text"]= str(res1) + "kg"
    lal2["text"]=str(res2)+"lbs"


    if res1 < 20800: 
        lal1.grid(column=0,row=2)
        lal2.grid(column=2,row=2)
        lal5.grid_remove()
    elif res1 > 20800:
        lal1.grid_remove()
        lal2.grid_remove()
        lal5.grid(column=1,row=2)



L1 = Label(top, text="flight time in hours:")
L1.grid(column=0,row=0)
E1 = Entry(top, bd =5, width=20)
E1.grid(column=2,row=0)

B1 = Button(top, text="Boeing 737", command=boeing_burn)
B1.grid(column=0,row=1)

B2 = Button(top, text="Airbus A320", command=a320_burn)
B2.grid(column=3,row=1)




top.mainloop()
ayylmao6
  • 11
  • 1
  • First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759) – stovfl Mar 29 '20 at 15:17

1 Answers1

0

Every time you click the button you are creating new labels. Instead you should create the labels outside the function and edit them inside

from tkinter import *

top = Tk()
top.title("fuel planner")

lal1 = Label(top, text = " kg")
lal2 = Label(top, text = " lbs")
lal5 = Label(top, text = "out of range")



def boeing_burn():

    res1=(float(E1.get()))* 2530
    res2=round(res1*2.204,2)

    lal1["text"] = str(res1) + "kg"
    lal2["text"] = str(res2) + "lbs"

    if res1 < 20800:
        lal1.grid(column=0,row=2)
        lal2.grid(column=2,row=2)
        lal5.grid_remove()
    elif res1 > 20800:
        lal1.grid_remove()
        lal2.grid_remove()
        lal5.grid(column=1,row=2)

def a320_burn():


    res1=(float(E1.get()))*2430
    res2=round(res1*2.204,2)

    lal1["text"] = str(res1) + "kg"
    lal2["text"] = str(res2) + "lbs"

    if res1 < 20800:
        lal1.grid(column=0,row=2)
        lal2.grid(column=2,row=2)
        lal5.grid_remove()
    elif res1 > 20800:
        lal1.grid_remove()
        lal2.grid_remove()
        lal5.grid(column=1,row=2)



L1 = Label(top, text="flight time in hours:")
L1.grid(column=0,row=0)
E1 = Entry(top, bd =5, width=20)
E1.grid(column=2,row=0)

B1 = Button(top, text="Boeing 737", command=boeing_burn)
B1.grid(column=0,row=1)

B2 = Button(top, text="Airbus A320", command=a320_burn)
B2.grid(column=3,row=1)
top.mainloop()
Jordan
  • 525
  • 1
  • 4
  • 19