在Tkinter中循环(Python)

时间:2012-12-20 12:20:46

标签: python-2.7 tkinter

这只是一直在冻结。 while循环没有运行,我认为这是问题

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()

1 个答案:

答案 0 :(得分:2)

这段代码就是问题:

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永远不会改变。它将始终为零,因此循环永远不会终止。

(您还有一个错误,即您使用0.0作为起始索引,但是tkinter文本索引应该是字符串,并且行号从1开始计数,而不是零。)