Python 循环计算完成后,才会显示循环的进度条

Python 循环计算完成后,才会显示循环的进度条,python,tkinter,Python,Tkinter,我很难理解进度条在循环上下文中是如何工作的。我的例子如下: import time from tkinter import ttk,Tk, Label, Button,Label,DoubleVar MAX = 4 root = Tk() root.title("My Progressbar") root.geometry('500x500') theLabel = Label(root, text="Progress") theLabel.pack

我很难理解进度条在循环上下文中是如何工作的。我的例子如下:

import time
from tkinter import ttk,Tk, Label, Button,Label,DoubleVar
MAX = 4

root = Tk()

root.title("My Progressbar")
root.geometry('500x500')

theLabel = Label(root, text="Progress")
theLabel.pack()

progress_var = DoubleVar() 
progress_var.set(0)
progressbar = ttk.Progressbar(root, variable=progress_var, 
length=400,maximum=MAX,mode='determinate')
progressbar.pack()


for i in range(MAX+1):
    print(i) #Where I will eventually do all my work. Here I print i and pause .5 sec
    time.sleep(.5)
    progress_var.set(i)
    root.update_idletasks()

# Add quit button
def _quit():
        root.quit()
        root.destroy()

quit_button = Button(master=root, text="Quit", bg='lightgray',command=_quit)
quit_button.pack()

root.mainloop()
我遗漏了一些明显的东西,因为直到循环完成后,该条才会出现。此外,是否可以在条形图上的某个位置指示完成百分比?

在执行流有机会到达
root.mainloop
之前,您已经完成了“工作”。您将希望在启动主循环后模拟执行工作。也许你可以添加一个按钮,点击后可以模拟工作?试试这个:

# ...

def do_work():
    for i in range(MAX+1):
        print(i) #Where I will eventually do all my work. Here I print i and pause .5 sec
        time.sleep(.5)
        progress_var.set(i)
        root.update_idletasks()

# ...

do_work_button = Button(master=root, text="Do Work", command=do_work)
do_work_button.pack()

root.mainloop()

如果只想每0.5秒增加进度条中的值,请尝试以下操作:

# ...

def do_work():
    for i in range(MAX+1):
        print(i) #Where I will eventually do all my work. Here I print i and pause .5 sec
        time.sleep(.5)
        progress_var.set(i)
        root.update_idletasks()

# ...

do_work_button = Button(master=root, text="Do Work", command=do_work)
do_work_button.pack()

root.mainloop()
import time
from tkinter import ttk,Tk, Label, Button,Label,DoubleVar
MAX = 4

root = Tk()

root.title("My Progressbar")
root.geometry('500x500')

theLabel = Label(root, text="Progress")
theLabel.pack()

progress_var = DoubleVar()
progress_var.set(0)
progressbar = ttk.Progressbar(root, variable=progress_var,
length=400,maximum=MAX,mode='determinate')
progressbar.pack()

def add():
    if progress_var.get() < MAX:
        print(progress_var.get())
        progress_var.set(progress_var.get()+1)
    root.after(500, add)

# Add quit button
def _quit():
    root.quit()
    root.destroy()

quit_button = Button(master=root, text="Quit", bg='lightgray',command=_quit)
quit_button.pack()

root.after(500, add)

root.mainloop()
导入时间
从tkinter导入ttk、Tk、标签、按钮、标签、DoubleVar
最大值=4
root=Tk()
root.title(“我的进度条”)
根几何体('500x500')
标签=标签(root,text=“Progress”)
标签包()
progress_var=DoubleVar()
进度变量集(0)
progressbar=ttk.progressbar(根,变量=progress\u变量,
长度=400,最大值=最大值,模式=确定)
progressbar.pack()
def add():
如果进度变量get()


如果这是一项特定的工作,您需要创建线程或进程。

是的,我应该在我的示例上进行扩展。在我的最终应用程序中,有一个您描述的“do_work”函数。我在progressbar.pack()之后和退出按钮之前添加了“do_work”,但同样的情况也发生了。此外,第二次点击“do_work”不会“刷新”进度条。请编辑到上面。当我点击“do_work”时会出现进度条,但在执行循环时不会显示渐进进度。谢谢。看起来我需要按照您的指示创建一个线程或进程。最后,我正在运行一个包含循环的脚本,这可能需要几个小时才能执行。