How to get a child thread to close when main GUI window is closed in pyqt5 / python 3?

你。 提交于 2020-12-15 07:20:10

问题


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 application. In this example, the child thread is a simple counter. When I close the main GUI, the counter still keeps going. How can I get the thread to end when the GUI window is closed? In the real case I may have a thread that is running operations that takes a few minutes to execute. I am reluctant to use a flag within the thread to assess if it should end because it may take minutes for the thread to close after the GUI window has been closed. I would prefer the thread to end right away. Any suggestions? Thank you.

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import (QWidget, QApplication,QPushButton, 
                             QVBoxLayout)
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 = threading.Thread(target=Worker)
        self.worker.daemon = True
        self.worker.start()

def Worker(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_()

回答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_()



回答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_()


来源:https://stackoverflow.com/questions/61151313/how-to-get-a-child-thread-to-close-when-main-gui-window-is-closed-in-pyqt5-pyt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!