Juan Carlos Asuncion
Juan Carlos Asuncion

Reputation: 136

Python pyqt pulsing progress bar with multithreading

Please bear with my question as I am a beginner. I have been having problems implementing the progress bar in pyqt and all of the example I have seen doesn't really explain on how to implement it properly and from this example and this example I somewhat partially made it work but it still hangs. I have this code:

class Window(QtGui.QMainWindow):
    def __init__(self):
         super(Window, self).__init__()
        self.setGeometry(750, 450, 400, 200)
        self.setFixedSize(self.size())
        btn1 = QtGui.QPushButton("Convert", self)
        btn1.move(210,171)
        btn1.clicked.connect(self.progbar)

    def progbar (self):
        self.prog_win = QDialog()
        self.prog_win.resize(400, 100)
        self.prog_win.setFixedSize(self.prog_win.size())
        self.prog_win.setWindowTitle("Processing request")
        self.lbl = QLabel(self.prog_win)
        self.lbl.setText("Please Wait.  .  .")
        self.lbl.move(15,18)
        self.progressBar = QtGui.QProgressBar(self.prog_win)
        self.progressBar.resize(410, 25)
        self.progressBar.move(15, 40)
        self.progressBar.setRange(0,1)
        self.myLongTask = TaskThread()

        #I think this is where I am wrong
        #because all of the answers here is very specific 
        #or just not for beginners
        self.prog_win.show()
        self.myLongTask.taskFinished.connect(self.onStart)
        self.output_settings()

    def onStart(self): 
        self.progressBar.setRange(0,0)
        self.myLongTask.start()

    def output_convert(self):
        #very long process to convert a txt file to excel

#My Thread
class TaskThread(QtCore.QThread):
    taskFinished = QtCore.pyqtSignal()
    def run(self):
        time.sleep(3)
        self.taskFinished.emit()

def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    app.exec_()
run()

All of the examples and posts here have been very helpful on understanding progress bar implementation but with all the example having specific answers for a specific problem I can't understand the implementation of progress bar in a standard pyqt app. could you guys at least point me in the right direction? Would be appreciated.

Upvotes: 3

Views: 10623

Answers (2)

Vivek Joshy
Vivek Joshy

Reputation: 934

This is a very basic progress bar that only uses what is needed at the bare minimum.

It would be wise to read this whole example to the end.

import sys
import time

from PyQt5.QtWidgets import (QApplication, QDialog,
                             QProgressBar, QPushButton)

TIME_LIMIT = 100

class Actions(QDialog):
    """
    Simple dialog that consists of a Progress Bar and a Button.
    Clicking on the button results in the start of a timer and
    updates the progress bar.
    """
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Progress Bar')
        self.progress = QProgressBar(self)
        self.progress.setGeometry(0, 0, 300, 25)
        self.progress.setMaximum(100)
        self.button = QPushButton('Start', self)
        self.button.move(0, 30)
        self.show()

        self.button.clicked.connect(self.onButtonClick)

    def onButtonClick(self):
        count = 0
        while count < TIME_LIMIT:
            count += 1
            time.sleep(1)
            self.progress.setValue(count)

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

The progress bar is first imported like so from PyQt5.QtWidgets import QProgressBar

Then it is initialized like any other widget in QtWidgets

The line self.progress.setGeometry(0, 0, 300, 25) method defines the x,y positions on the dialog and width and height of the progress bar.

We then move the button using .move() by 30px downwards so that there will be a gap of 5px between the two widgets.

Here self.progress.setValue(count) is used to update the progress. Setting a maximum value using .setMaximum() will also automatically calculated the values for you. For example, if the maximum value is set as 50 then since TIME_LIMIT is 100 it will hop from 0 to 2 to 4 percent instead of 0 to 1 to 2 every second. You can also set a minimum value using .setMinimum() forcing the progress bar to start from a given value.

Executing this program will produce a GUI similar to this.

Progress Bar Dialog Not Responding

As you can see, the GUI will most definitely freeze and be unresponsive until the counter meets the TIME_LIMIT condition. This is because time.sleep causes the OS to believe that program has become stuck in an infinite loop.

QThread

So how do we overcome this issue ? We can use the threading class that PyQt5 provides.

import sys
import time

from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import (QApplication, QDialog,
                             QProgressBar, QPushButton)

TIME_LIMIT = 100

class External(QThread):
    """
    Runs a counter thread.
    """
    countChanged = pyqtSignal(int)

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

class Actions(QDialog):
    """
    Simple dialog that consists of a Progress Bar and a Button.
    Clicking on the button results in the start of a timer and
    updates the progress bar.
    """
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Progress Bar')
        self.progress = QProgressBar(self)
        self.progress.setGeometry(0, 0, 300, 25)
        self.progress.setMaximum(100)
        self.button = QPushButton('Start', self)
        self.button.move(0, 30)
        self.show()

        self.button.clicked.connect(self.onButtonClick)

    def onButtonClick(self):
        self.calc = External()
        self.calc.countChanged.connect(self.onCountChanged)
        self.calc.start()

    def onCountChanged(self, value):
        self.progress.setValue(value)

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

Let's break down these modifications.

from PyQt5.QtCore import QThread, pyqtSignal

This line imports Qthread which is a PyQt5 implementation to divide and run some parts(eg: functions, classes) of a program in the background(also know as multi-threading). These parts are also called threads. All PyQt5 programs by default have a main thread and the others(worker threads) are used to offload extra time consuming and process intensive tasks into the background while still keeping the main program functioning.

The second import pyqtSignal is used to send data(signals) between worker and main threads. In this instance we will be using it to tell the main thread to update the progress bar.

Now we have moved the while loop for the counter into a separate class called External.

class External(QThread):
    """
    Runs a counter thread.
    """
    countChanged = pyqtSignal(int)

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

By sub-classing QThread we are essentially converting External into a class that can be run in a separate thread. Threads can also be started or stopped at any time adding to it's benefits.

Here countChanged is the current progress and pyqtSignal(int) tells the worker thread that signal being sent is of type int. While, self.countChanged.emit(count) simply sends the signal to any connections in the main thread(normally it can used to communicate with other worker threads as well).

def onButtonClick(self):
        self.calc = External()
        self.calc.countChanged.connect(self.onCountChanged)
        self.calc.start()

def onCountChanged(self, value):
    self.progress.setValue(value)

When the button is clicked the self.onButtonClick will run and also start the thread. The thread is started with .start(). It should also be noted that we connected the signal self.calc.countChanged we created earlier to the method used to update the progress bar value. Every time External::run::count is updated the int value is also sent to onCountChanged.

This is how the GUI could look after making these changes.

QThread Progress Bar

It should also feel much more responsive and will not freeze.

Upvotes: 10

Juan Carlos Asuncion
Juan Carlos Asuncion

Reputation: 136

The answer to my own question. It is not that hard if you can understand the concept of threading and passing variables through classes. My first mistake was really the lack of knowledge about Worker threads, second is I thought that once you declare the Thread it means that you just have to call it so that it would run the function inside the main class so I was searching on how you would implement that and all I thought was wrong.

Solution

All the hard/Long processes SHOULD be in the subclassed QThread under def run and should be called in your class Window(QtGui.QMainWindow): or main loop and this is what my code look like now

class Window(QtGui.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(750, 450, 400, 200)
        self.setFixedSize(self.size())
        btn1 = QtGui.QPushButton("Convert", self)
        btn1.move(210,171)
        btn1.clicked.connect(self.progbar)

    def progbar (self):
        self.prog_win = QDialog()
        self.prog_win.resize(400, 100)
        self.prog_win.setFixedSize(self.prog_win.size())
        self.prog_win.setWindowTitle("Processing request")
        self.lbl = QLabel(self.prog_win)
        self.lbl.setText("Please Wait.  .  .")
        self.lbl.move(15,18)
        self.progressBar = QtGui.QProgressBar(self.prog_win)
        self.progressBar.resize(410, 25)
        self.progressBar.move(15, 40)
        self.progressBar.setRange(0,1)

        self.myLongTask = TaskThread(var = DataYouWantToPass) #initializing and passing data to QThread
        self.prog_win.show()
        self.onStart() #Start your very very long computation/process
        self.myLongTask.taskFinished.connect(self.onFinished) #this won't be read until QThread send a signal i think

    def onStart(self): 
        self.progressBar.setRange(0,0)
        self.myLongTask.start()

    #added this function to close the progress bar
    def onFinished(self):
        self.progressBar.setRange(0,1)
        self.prog_win.close()


#My Thread
class TaskThread(QtCore.QThread):
    taskFinished = QtCore.pyqtSignal()

    #I also added this so that I can pass data between classes
    def __init__(self, var, parent=None):
        QThread.__init__(self, parent)
        self.var = var

    def run(self):
        #very long process to convert a txt file to excel 

def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    app.exec_()
run()

If something in this answer is wrong then please correct me as it would be a great help to understand it more or maybe some dos and don't

Upvotes: 1

Related Questions