【问题标题】:How to safely quit pyqt application python如何安全退出pyqt应用程序python
【发布时间】:2020-06-13 05:25:16
【问题描述】:

我有一个pyqt5 项目。我以全屏模式启动窗口。它正在工作,但是因为它是全屏的,所以我不能点击x 按钮来关闭它,所以我必须按alt-f4 来关闭它。它工作正常,但现在我在项目中运行了另一个类,因此当我按下alt-f4 时,它会关闭窗口但仍然看起来线程没有关闭,因为它停留在终端中。下面是代码:

class RecognizeFaceInFrame(Thread):

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

        self.get_face_name = False
        self.stop_face_thread = False

    def recognize_face_frame(self):
        try:
            if self.get_face_name:
                #SOME CODE
            time.sleep(1)
        except Exception as e:
            print("Exception occurred in recognize face {}".format(e))

    def run(self):
        while not self.stop_face_thread:
            self.recognize_face_frame()

class TRIANGLE(QMainWindow, Ui_MainWindow):
    def __init__(self):
        # SOME CODE
        self.showFullScreen()

        self.timer = QTimer()
        self.timer.timeout.connect(self.view_cam)
        self.timer.start(20)
        self.frame_count = 0

        self.face_recog_thread = RecognizeFaceInFrame()  
        self.face_recog_thread.start()

    def __del__(self):
        self.timer.stop()
        self.face_recog_thread.stop_face_thread = True

    def view_cam(self):
        # SOME CODE


app = QApplication(sys.argv)
app.setStyle('Windows')
main_window = TRIANGLE()
main_window.show()
sys.exit(app.exec_())

我有一个 RecognizeFaceInFrame 类,我在 TRIANGLE 类中的 __init__ 中初始化它。如果我们将get_face_name 设置为True,函数recognize_face_frame 将开始执行。如果我们将stop_face_thread 设置为True,该线程将自动关闭,因此我已将其放入TRAINGLE__del__,但是当我按下alt-f4 时它不会关闭。谁能帮我理解我应该在这里做什么来安全地关闭所有线程和应用程序。请帮忙。谢谢

【问题讨论】:

    标签: python multithreading class pyqt5


    【解决方案1】:

    closeEvent怎么样?

    继承 Qt 模块的类被 Qt 自己的 garbage collector 删除 - 至少链接是这样说的。因此,UI Object 不会立即被删除。

    将 PySide2 更改为 PyQt 并尝试一下。一旦你按下 alt+f4,函数 closeEvent 就会运行。在此之前,线程将继续向控制台打印消息。

    from PySide2.QtWidgets import QWidget, QApplication, QTextEdit, QVBoxLayout
    from PySide2.QtGui import QCloseEvent
    from threading import Thread
    import sys
    import time
    
    
    class TestThread(Thread):
    
        def __init__(self):
            super().__init__()
            self.stop_thread = False
    
        def run(self):
            while not self.stop_thread:
                print("I'm alive!")
                time.sleep(1)
    
            print("Dead! Not a big surprise.")
    
    
    class MainWindow(QWidget):
        def __init__(self):
            super(MainWindow, self).__init__()
    
            self.test_thread = TestThread()
            self.test_thread.start()
    
        def closeEvent(self, event:QCloseEvent):
            self.test_thread.stop_thread = True
    
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        window = MainWindow()
        window.show()
        sys.exit(app.exec_())
    

    结果:

    我还活着!
    我还活着!
    我还活着!
    死的!不是什么大惊喜。

    进程以退出代码 -1 结束

    【讨论】:

      【解决方案2】:

      您还可以使用来自 QApplication 的aboutToQuit 信号。只需将其连接到您的清理方法即可。 See docs.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-22
        • 1970-01-01
        • 1970-01-01
        • 2010-11-27
        相关资源
        最近更新 更多