Tkinter 中的 while 循环(Python) [英] While loop in Tkinter (Python)

查看:43
本文介绍了Tkinter 中的 while 循环(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

这段代码有问题:

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

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

(你也有一个错误,你使用 0.0 作为起始索引,但 tkinter 文本索引应该是字符串,行号从 1 开始计数,而不是 0.)

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

这篇关于Tkinter 中的 while 循环(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆