Search code examples
pythontkinterprogress-bar

ttk Progressbar display only in the end of the process


As per object, the progress bar is displayed only at the end of the completion of the for loop. Instead I would like it to show the progress of the cycle step by step.

from tkinter import ttk
from tkinter import *
import time

def inner_loop_func():
    k = 0
    for i in range(10**5):
        k=k+1
    print("k: ",k)

def loop_fun():

    p = ttk.Progressbar(root, orient="horizontal", length=300, mode="determinate", takefocus=True, maximum=100)
    p['value'] = 0
    p.pack()

    end = 100
    for i in range(end):
        start(end,p)
        inner_loop_func()
        print(i," of ", end)

def start(end,p):

    if p['value'] < 300:
        p['value'] += (300/end)
    else:
        print("finish")

if __name__ == "__main__":

    root = Tk()
    loop_fun()
    root.mainloop()

Solution

  • Use p.update() inside the loop_fun function for loop:

    for i in range(end):
        start(end, p)
        inner_loop_func()
        print(i, " of ", end)
        p.update()