I need to display an indeterminate progress bar to indicate to the user that something is happening.
I have found many examples but all are operated by a button and none shows how to display a progress bar while a function is performed. In addition, I want to display the progress bar when the function starts and disappear it at the end of the function, without the user having to press any button or close the window. My main problem is that I can only display the progress bar if I use the mainloop()
function and in this case my program is blocked until I close the window. as advised, I tried to start the progress bar in a different thread, currently without success
This is my code :
main.py
import time
import ProgressBar
print("start")
print("sleep 5 seconds")
pb = ProgressBar.ProgBar()
time.sleep(5)
pb.stop()
print("end")
ProgressBar.py
from tkinter import *
from tkinter import ttk
from threading import thread
class ProgBar(Frame):
def __init__(self):
Frame.__init__(self)
self.master.title("ProgressBar example")
self.grid(sticky=E+W+N+S)
self.pbar = ttk.Progressbar(self, orient="horizontal", length=300, mode="indeterminate", maximum=100)
self.pbar.grid(row=0, column=0, pady=2, padx=2, sticky=E+W+N+S, columnspan=3)
self.start()
def start(self):
def start_thread(self):
self.pbar.start()
self.mainloop()
Thread(target=start_thread, args=(self,)).start()
def stop(self):
self.pbar.stop()
self.destroy()