I have a PyQT GUI application progress_bar.py
with a single progressbar and an external module worker.py
with a process_files()
function wh
Make the process_files
function a generator function that yields a value (the progress value) and pass it as a callback to a method in your Window
class that updates the progress bar value. I have added a time.sleep
call in your function so you can observe the progress:
import time
from worker import process_files
class Window(QtGui.QMainWindow):
def __init__(self):
...
def observe_process(self, func=None):
try:
for prog in func():
self.progress.setValue(prog)
except TypeError:
print('callback function must be a generator function that yields integer values')
raise
app = QtGui.QApplication(sys.argv)
GUI = Window()
# process files and report progress using .setValue(percent)
GUI.observe_process(process_files)
sys.exit(app.exec_())
worker.py
def process_files():
file_list = ['file1', 'file2', 'file3']
counter = 0
for file in file_list:
counter += 1
percent = 100 * counter / len(file_list)
time.sleep(1)
yield percent
Result:
After processing file2