【问题标题】:How to get a child thread to close when main GUI window is closed in pyqt5 / python 3?在pyqt5 / python 3中关闭主GUI窗口时如何关闭子线程?
【发布时间】:2025-12-21 20:25:16
【问题描述】:

我正在使用 pyqt5 (Python 3.6) 编写 GUI。我正在尝试与主 GUI 并行运行另一个线程。我希望这个子线程在我关闭主应用程序时终止。在这个例子中,子线程是一个简单的计数器。当我关闭主 GUI 时,计数器仍在继续。当 GUI 窗口关闭时,如何让线程结束?在实际情况下,我可能有一个线程正在运行需要几分钟才能执行的操作。我不愿意在线程中使用标志来评估它是否应该结束,因为在 GUI 窗口关闭后线程可能需要几分钟才能关闭。我希望线程立即结束。有什么建议? 谢谢。

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

【问题讨论】:

  • 嗯,对于初学者来说,即使在 Python 中使用 QThread 使用 Qt 自己的线程总是一件好事。然后,还有terminate(),但在线程运行时杀死它通常不是一个好主意,除非你真的知道你在做什么,在任何的情况下。
  • 我尝试将 terminate (self.worker.terminate()) 添加到应用程序的 closeEvent 中,但它会引发错误:AttributeError: 'Thread' object has no attribute 'terminate'。 def closeEvent(self,event): print('Closing') self.worker.terminate() event.accept()
  • 从您的回复中,我想说您仍在使用 Python 的 threading.Thread 对象,并且不是建议的 QThread 对象。
  • 确实如此。错过了,我会试试的。
  • 在当前示例中,您可以使用while count>0 and running: 和全局变量running = True。当您设置running = False 时,它应该停止循环,然后线程应该自动结束。但并非在所有情况下都是解决方案。

标签: python multithreading pyqt5


【解决方案1】:
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_()

【讨论】:

  • 你也可以为你的代码提供一些解释-FROM REVIEW
【解决方案2】:

我尝试使用 QThread,但这会锁定主 GUI。我不确定我是否正确实施。

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

【讨论】: