Legend_recalls
Legend_recalls

Reputation: 273

how to create a simple progress bar loop in tkinter

Since I can't find an exact thread like this elsewhere I will post this question here, I just started learning Tkinter and facing problems with the progress bar widget.

from tkinter import ttk
import time
root=Tk()
p = ttk.Progressbar(root,orient=HORIZONTAL,length=200,mode="determinate",takefocus=True,maximum=100)
p['value']=0
p.pack()
for x in range(5):
    p['value']+=20
    root.update_idletasks()
    root.after(1000)
root.mainloop()

I can't find the exact problem I first tried time.sleep it doesn't work then I was told to use 'after' it still doesn't work I can make a looping progress bar ar with a function and button but i really wanna start the loading bar just as the Tkinter starts.

hope someone points out the problem, Thank you!!

Upvotes: 0

Views: 6743

Answers (3)

user15801675
user15801675

Reputation:

Try this. I have used a function to start the progress bar.

from tkinter import ttk
from tkinter import *
import time

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

def start():
    if p['value'] < 100:
        p['value'] += 20

        root.after(1000, start)

root.after(1000, start)
root.mainloop()

Upvotes: 4

pukiwawa
pukiwawa

Reputation: 1

maybe you can try thread.. not sure if this is what u want. as follows

from tkinter import *
from tkinter.ttk import *
import time, _thread

def progressbar(parent):
    pb = Progressbar(parent, length=100, mode='indeterminate', maximum=100, value=50)
    pb.pack(padx=2, pady=2, expand=YES, fill=X)
    return pb
    
def run(pb):
    pb.update()

def test(val, pb):
    for i in range(val):
        print(i)
        time.sleep(0.01)
    pb.stop()   

def double(pb):
    _thread.start_new_thread(run, (pb,))
    _thread.start_new_thread(test, (200, pb))

root = Tk()
global pb
a = progressbar(root)
a.start()
double(a)
root.mainloop()

Upvotes: 0

MHP
MHP

Reputation: 362

Threading is another good way to use with progress bar in tkinter, just make sure you update the window every times.

from tkinter import ttk, Tk
from tkinter import *
import time, threading
root=Tk()
p = ttk.Progressbar(root,orient=HORIZONTAL,length=200,mode="determinate",takefocus=True,maximum=100)
p['value']=0
p.pack()
def update():
    for x in range(5):
        p['value']+=20
        root.update()
        time.sleep(1)
threading.Thread(target=update).start()
root.mainloop()

Upvotes: 0

Related Questions