首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在pyqt5 /Python3中,当主GUI窗口关闭时,如何使子线程关闭?

在pyqt5 /Python3中,当主GUI窗口关闭时,如何使子线程关闭?
EN

Stack Overflow用户
提问于 2020-04-11 10:18:35
回答 2查看 548关注 0票数 0

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

代码语言:javascript
复制
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_()
EN

回答 2

Stack Overflow用户

发布于 2020-04-11 11:04:47

我试图使用QThread,但这锁定了主图形用户界面。我不确定我是否正确地实现了它。

代码语言:javascript
复制
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_()
票数 0
EN

Stack Overflow用户

发布于 2020-12-01 23:42:57

代码语言:javascript
复制
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_()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61151313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档