I have a PyQT GUI application progress_bar.py
with a single progressbar and an external module worker.py
with a process_files()
function which does some routine with a list of files and reports current progress using percent
variable.
What I want to do is to report the current progress of the worker.process_files
using QProgressBar.setValue()
method, but I have no idea how to implement it (callback function or something?)
Here are my modules:
progress_bar.py
import sys
from PyQt4 import QtGui
from worker import process_files
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(100, 100, 300, 100)
self.progress = QtGui.QProgressBar(self)
self.progress.setGeometry(100, 50, 150, 20)
self.progress.setValue(0)
self.show()
app = QtGui.QApplication(sys.argv)
GUI = Window()
# process files and report progress using .setValue(percent)
process_files()
sys.exit(app.exec_())
worker.py
def process_files():
file_list = ['file1', 'file2', 'file3']
counter = 0
for file in file_list:
# do_stuff_with_the_file
counter += 1
percent = 100 * counter / len(file_list)
print percent
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 yourWindow
class that updates the progress bar value. I have added atime.sleep
call in your function so you can observe the progress:worker.py
Result:
After processing
file2