user6551302
user6551302

Reputation:

Manual progress bar python

Im trying to make a code so that it displays all numbers from 1 to 100 to show as its loading something.

for i in range(101):
        self.new = Label(self.label_progress, text=i)
        time.sleep(1)
        self.new.place(in_= self.label_progress)
        if i == 100:
            self.new1=Label(self.label_progress, text="the download is complete")
            self.new1.place(in_=self.label_progress, x=50)

but it seems like it doesnt want to show each number untill the loop is complete, at the end it just shows a 100. Any suggestions on how to fix it?

Upvotes: 0

Views: 284

Answers (1)

furas
furas

Reputation: 142641

tkinter has mainloop() which runs all the time and does many thing - ie. it updates data in widget, redraws widgets, executes your function. When mainloop() executes your function then it can't updates/redraws widgets till your function stop running. You can use root.update() in your function to force tkinter to update/readraw widgets.

Or you can use

root.after(miliseconds, function_name) 

to periodically execute function which will update Label . And between executions tkinter will have time to update/redraw widgets.

Examples which use after to update time in Label:

https://github.com/furas/my-python-codes/tree/master/tkinter/timer-using-after


BTW: tkinter has ttk.Progressbar

Example code: https://stackoverflow.com/a/24770800/1832058

enter image description here

Upvotes: 1

Related Questions