Hello. As from the topic title, i am trying to work a multithread GUI where the progress bar indicates the running process completion (like we always see when we are installing/uninstalling/downloading). I've read a couple of written tutorials and some other forums discussions regarding the progress bar and multithreading. I've also even tried the simple multithreading examples given but I still don't get it especially adding the process that i want to run.

This is my GUI (simple) window.JPG :

class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.progressBar.setVisible(False) #I WASN'T SUCCESSFULL IN MANUALLY CREATING A PROGRESS BAR BUT THATS ANOTHER ISSUE ILL DEAL WITH LATER
        self.btn_browse.clicked.connect(self.OpenFile)
        self.btn_hash.clicked.connect(self.CalcHash)

    def OpenFile(self):
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","Raw Files (*.dng)", options=options)
        if fileName:
            self.txt_filename.setText(fileName)

    def CalcHash(self):
        self.progressBar.setVisible(True)
        ##THE PROCESS
        ##THE PROCESS IS SUPPOSE TO BE HERE RIGHT??

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

so then i need a "worker" class (the new thread), where the progress bar (so far all the example says that) process will be run.

class TheThread(QThread):
    #signals to communicate to the main program class
    countChanged = pyqtSignal(int)

    def run(self):
        count = 0
        while count < TIME_LIMIT:
            count +=15
            time.sleep(1)
            self.countChanged.emit(count)

i need something like this in my CalcHash func:

self.worker = TheThread()
self.worker.updateProgress.connect(self.onProgress)
self.worker.start()

But the progress bar doesn't "progress" just blank while my calc hash runs. Please help. Thank you in advance.

update : I got both my process and progress bar to work but how do i make it sync? like the progress bar processes/completes at same time as my running process?

This is my full code :

import hashlib, time, sys
from PyQt5 import (QtGui, QtWidgets) 
from PyQt5.QtWidgets import (QApplication, QWidget, QInputDialog, QMainWindow, QFileDialog, QProgressBar, QLabel)
from PyQt5.QtCore import (QObject, QRunnable, pyqtSignal, pyqtSlot, QThreadPool, QTimer, QThread)
from PyQt5 import uic

qtCreatorFile = "multithread.ui"

Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

class CalculateHashThread(QThread):
    def __init__(self, filename):
        super().__init__()
        self.filename = filename

    def run(self):
        BLOCKSIZE = 65536
        hasher = hashlib.md5()
        today = time.strftime("%x") + " " +  time.strftime("%X")
        with open(self.filename, 'rb') as afile:
            buf = afile.read(BLOCKSIZE)
            while len(buf) > 0:
                hasher.update(buf)
                buf = afile.read(BLOCKSIZE)
        with open("AnalysisOfFile.txt", "w") as hashnum:
            hashnum.write("Analysed file : " + self.filename + "\n")
            hashnum.write("Analysis started at : "+ today + "\n\n")
            hashnum.write("Hash value is : " + hasher.hexdigest() + "\n\n")

class ProgressBarThread(QThread):
    updateProgress = pyqtSignal(int)

    def __init__(self):
        super().__init__()

    def run(self):
        for i in range(1,101):
            self.updateProgress.emit(i)

class MyApp(QMainWindow, Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.calculating.setVisible(False)
        self.progressBar.setVisible(False)
        self.btn_browse.clicked.connect(self.OpenFile)
        self.btn_hash.clicked.connect(self.CalcHash)

    def OpenFile(self):
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","Raw Files (*.raw)", options=options)
        if fileName:
            self.txt_filename.setText(fileName)

    def CalcHash(self):
        self.calculating.setVisible(True)
        self.progressBar.setVisible(True)

        filename = self.txt_filename.text()
        self.calchash = CalculateHashThread(filename)
        self.calchash.start()
        self.progress = ProgressBarThread()
        self.progress.start()
        self.progress.updateProgress.connect(self.progressBar.setValue)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.