Skip to content Skip to sidebar Skip to footer

How To Get A Child Thread To Close When Main Gui Window Is Closed In Pyqt5 / Python 3?

I am writing a GUI using pyqt5 (Python 3.6). I am trying to run another thread in parallel of the main GUI. I would like this child thread to terminate when I close the main applic

Solution 1:

I tried to use the QThread but this locks up the main GUI. I am not sure if I am implementing it correctly.

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import (QWidget, QApplication,QPushButton, 
                             QVBoxLayout)
from PyQt5.QtCore import QThread
import time, threading, sys

class testScriptApp(QtWidgets.QWidget):

    def __init__(self, parent=None):
        # initialize th widget
        QtWidgets.QWidget.__init__(self, parent)
        # set the window title
        self.setWindowTitle("Scripting")
        # manage the layout
        self.mainGrid = QVBoxLayout()
        self.button = QPushButton('Start')
        self.button.clicked.connect(self.on_click)
        self.mainGrid.addWidget(self.button)
        self.setLayout(self.mainGrid)

    def on_click(self):
        self.worker = Worker()
        self.worker.run()

    def closeEvent(self,event):
        print('Closing')
        self.worker.terminate()
        event.accept()

class Worker(QThread):

    def __init__(self):
        QThread.__init__(self)

    def run(self):
        count=1
        while count>0:
            print(count)
            time.sleep(2)
            count+=1

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    myapp = testScriptApp()
    myapp.show()
    app.exec_()

Solution 2:

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import (QWidget, QApplication,QPushButton, 
                             QVBoxLayout)
from PyQt5.QtCore import QThread,QObject
import time, threading, sys

class testScriptApp(QtWidgets.QWidget):

    def __init__(self, parent=None):
        # initialize th widget
        QtWidgets.QWidget.__init__(self, parent)
        # set the window title
        self.setWindowTitle("Scripting")
        # manage the layout
        self.mainGrid = QVBoxLayout()
        self.button = QPushButton('Start')
        self.button.clicked.connect(self.on_click)
        self.mainGrid.addWidget(self.button)
        self.setLayout(self.mainGrid)

    def on_click(self):
        self.my_thread = QThread()
        self.worker = Worker()
        self.worker.moveToThread(self.my_thread)
        self.my_thread.started.connect(self.worker.run)
        self.my_thread.start()        

    def closeEvent(self,event):
        print('Closing')

class Worker(QObject):

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

    def run(self):
        count=1
        while count>0:
            print(count)
            time.sleep(2)
            count+=1

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    myapp = testScriptApp()
    myapp.show()
    app.exec_()

Post a Comment for "How To Get A Child Thread To Close When Main Gui Window Is Closed In Pyqt5 / Python 3?"