Keep PyQt UI Responsive With Threads
<p>This is my first question here so please bear with me.</p>

<p>I have created a relatively complex PyQt program and am trying to implement threads so that when the program encounters a part of the program which is particularly CPU intensive, the GUI will remain refreshed and responsive throughout. Sadly though, I am having some difficulties with the threading.</p>

<p>I am using Python 2.7 for reasons that I don't believe to be relevant.</p>

<p>Anyway, the entire program runs within one class and calls upon a PyQt designer .ui file in order to display the actual GUI. When a particular button is pressed, in order to shred a file, it calls a function within that class that then starts a thread using the 'thread' module, yes, outdated, I know. The shredding function that is then called from this commences the shredding of the file. Throughout the shredding of the file, the actual shredding function interacts and adds bits to the GUI in order to keep the user up to date on what is happening.</p>

<p>During the execution of the function the GUI continues to be refreshed, however it does become a little laggy, I can cope with that. However, when that function is complete, instead of smoothly continuing and allowing the user to keep using the program, the program throws a complete hissy fit and simply just stops working and has to be closed.</p>

<p>Hopefully someone can assist me here. I would greatly appreciate as much detail as possible as I have been searching around for a way to cope with this for a good number of weeks now.</p>

<p>Edit: I should clarify that I am using PyQt4.</p>

<p>Thanks,
BoshJailey</p>

<p>Here's a simple demo of threading in pyqt5. Qt has it's own threading class that works pretty well.</p>

<pre><code>from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSignal
import sys
import time

class TheBoss(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(TheBoss, self).__init__(parent)
        self.resize(300,200)
        self.VL = QtWidgets.QVBoxLayout(self)
        self.label = QtWidgets.QLabel()
        self.VL.addWidget(self.label)
        self.logger = Logger()
        self.logger.sec_signal.connect(self.label.setText)
        self.logger.start()

    def closeEvent(self,event):
        self.logger.terminate()

class Logger(QtCore.QThread):
    sec_signal = pyqtSignal(str)
    def __init__(self, parent=None):
        super(Logger, self).__init__(parent)
        self.current_time = 0
        self.go = True
    def run(self):
        #this is a special fxn that's called with the start() fxn
        while self.go:
            time.sleep(1)
            self.sec_signal.emit(str(self.current_time))
            self.current_time += 1

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    app.setApplicationName("Thread Example")
    window = TheBoss()
    window.show()
    sys.exit(app.exec_())
</code></pre>

python multithreading user-interface crash python-multithreading