How to update a progress bar in a loop?

What is the easy method to update Tkinter progress bar in a loop?

I need a solution without much mess, so I can easily implement it in my script, since it's already pretty complicated for me.

Let's say the code is:

from Tkinter import *
import ttk


root = Tk()
root.geometry('{}x{}'.format(400, 100))
theLabel = Label(root, text="Sample text to show")
theLabel.pack()


status = Label(root, text="Status bar:", bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, fill=X)

root.mainloop()

def loop_function():
    k = 1
    while k<30:
    ### some work to be done
    k = k + 1
    ### here should be progress bar update on the end of the loop
    ###   "Progress: current value of k =" + str(k)


# Begining of a program
loop_function()

Answers


Here's a quick example of continuously updating a ttk progressbar. You probably don't want to put sleep in a GUI. This is just to slow the updating down so you can see it change.

from Tkinter import *
import ttk
import time

MAX = 30

root = Tk()
root.geometry('{}x{}'.format(400, 100))
progress_var = DoubleVar() #here you have ints but when calc. %'s usually floats
theLabel = Label(root, text="Sample text to show")
theLabel.pack()
progressbar = ttk.Progressbar(root, variable=progress_var, maximum=MAX)
progressbar.pack(fill=X, expand=1)


def loop_function():

    k = 0
    while k <= MAX:
    ### some work to be done
        progress_var.set(k)
        k += 1
        time.sleep(0.02)
        root.update_idletasks()
    root.after(100, loop_function)

loop_function()
root.mainloop()

Need Your Help

Transparent Splash Screen (WinForms)

vb.net winforms splash-screen

I created a transparent splash screen in VB.Net 2012; it looks great, except one problem that it appear inside a white box for few seconds before it become transparent. The way I created it is by u...

Handling endianness of floating point values when there is no fixed size floating point type available

c++ floating-point cross-platform endianness floating-point-conversion

I'm writing a binary file reader/writer and have decided that to handle the issue of endianness I will convert all data to "network" (big) endianness on writing and to host endianness on reading. I'm