如何实时使用Progressbar小部件

时间:2017-05-10 01:38:25

标签: python-2.7 tkinter widget

我有以下使用Tkinter小部件的代码:

from Tkinter import *
from ttk import Progressbar

root = Tk()

def addThoseNumbers():
    y = 0
    for x in range(1000000):
        y += x
        if x % 10000.0 == 0:
            invoiceStatus['value'] = x/10000.0
    print y

invoiceStatus = Progressbar(root, length = 280, mode = 'determinate')
invoiceStatus.pack()
invoiceButton = Button(root, text = "Confirm", font = ("Helvetica", 10), \
command = addThoseNumbers)
invoiceButton.pack()

root.mainloop()

理想情况下,进度条会在程序工作时更新,以显示已完成的任务量,但是一旦任务完成,它只会从0%减少到100%。如何编写程序,以便进度条实时显示进度?

1 个答案:

答案 0 :(得分:1)

您只需要添加root.update()

from Tkinter import *
from ttk import Progressbar

root = Tk()

def addThoseNumbers():
    y = 0
    for x in range(1000000):
        y += x
        if x % 10000.0 == 0:
            invoiceStatus['value'] = x/10000.0
            root.update()
    print y

invoiceStatus = Progressbar(root, length = 280, mode = 'determinate')
invoiceStatus.pack()
invoiceButton = Button(root, text = "Confirm", font = ("Helvetica", 10), \
command = addThoseNumbers)
invoiceButton.pack()

root.mainloop()