I would like to implement a progress bar in Tkinter which fulfills the following requirements:
- The progress bar is the only element within the main window
- It can be started by a start command without the need of pressing any button
- It is able to wait until a task with unknown duration is finished
- The indicator of the progress bar keeps moving as long as the task is not finished
- It can be closed by a stop command without the need of pressing any stop bar
So far, I have the following code:
import Tkinter
import ttk
import time
def task(root):
root.mainloop()
root = Tkinter.Tk()
ft = ttk.Frame()
ft.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.TOP)
pb_hD = ttk.Progressbar(ft, orient='horizontal', mode='indeterminate')
pb_hD.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.TOP)
pb_hD.start(50)
root.after(0,task(root))
time.sleep(5) # to be replaced by process of unknown duration
root.destroy()
Here, the problem is that the progress bar does not stop after the 5s are over.
Could anybody help me finding the mistake?
Once the mainloop is active, the script wont move to the next line until the root is destroyed. There could be other ways to do this, but I would prefer doing it using threads.
Something like this,
Let me know if that helps.