Rajat Bansal
Rajat Bansal

Reputation: 1

While loop in Tkinter (Python)

This just keeps freezing. The while loop does not run and I think that's the problem

from Tkinter import*

def reveal():
    counter=0
    lowest=0
    Stotal=0
    i=0

    cost=float(cost_ent.get())
    if cost>0:
        lowest=cost
        counter+=1
        Stotal=cost+Stotal
    else:
        message="Invalid Number"
        txt.insert(0.0, message)
    while i==0:
        cost=float(cost_ent.get())
        if cost>0:
            counter+=1
            Stotal=cost+Stotal
            if cost<lowest:
                lowest=cost
        else:
            message="Invalid Number"
            txt.insert(0.0, message)

    message="The number of items:",counter,"\n"
    txt.insert (0.0, message)
    message="The subtotal is:",Stotal,"\n"
    txt.insert(0.0, message)
    message="The lowest item is:",lowest,"\n"
    txt.insert(0.0, message)
    message="The discount is:", discount,"\n"
    txt.insert(0.0, message)
    message="Before tax:", Stotal-discount,"\n"
    txt.insert(0.0, message)
    tax=Stotal*tax
    message="The tax is:",tax,"\n"
    txt.insert(0.0, message)
    message="The total is:",Stotal+tax,"\n"
    txt.insert(0.0, message)


    txt.delete(0.0, END)
root=Tk()
root.title("BOXING DAY SALE !!!!!!")
root.geometry("600x400")
app=Frame(root)
app.grid()

instl_lbl=Label(app,text = "Enter item cost")
instl_lbl.grid(row=1, column=1, sticky=W)
cost_ent=Entry(app)
cost_ent.grid(row=1, column=2, sticky=W)

bttn=Button(app, text="Enter", command=reveal)
bttn.grid(row=2, column=2, sticky=W)


txt=Text (app, width=50, height=10, wrap=WORD)
txt.grid(row=4, column=2, sticky=W)


root.mainloop()

Upvotes: 0

Views: 2565

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

This code is the problem:

while i==0:
    cost=float(cost_ent.get())
    if cost>0:
        counter+=1
        Stotal=cost+Stotal
        if cost<lowest:
            lowest=cost
    else:
        message="Invalid Number"
        txt.insert(0.0, message)

i is never changed. It will always be zero, so the loop never terminates.

(you also have a bug in that you're using 0.0 as the starting index, but tkinter text indexes should be strings, and the line numbers start counting at one, not zero.)

Upvotes: 2

Related Questions