closeEvent并不能真正关闭整个应用程序

我使用pyqt5创建了一个简单的python应用程序。该应用程序包括一个登录窗口,单击登录按钮后,会打开一个带有matplotlib图的GUI。 GUI在后台运行一些线程,在线程内部我打开了一些套接字,这些套接字可以转换绘图信息。

我发现此simpe closeEvent代码关闭了窗口应用程序:

    def closeEvent(self,event):
        reply = QMessageBox.question(QMessageBox(self),'Window Close','Are you sure you want to close the window?',QMessageBox.Yes | QMessageBox.No,QMessageBox.No)

        if reply == QMessageBox.Yes:
            event.accept()
            print('Window closed')
        else:
            event.ignore()

此功能有效,窗口已关闭,但是由于我在后台有带有套接字的线程,因此该线程一直在运行并且并未真正关闭(目前我需要手动将其关闭)。

这些是在GUI中运行的线程(它们不在ApplicationWindow类内,closeEvent在ApplicationWindow内部):

def company_commander_thread(company_num,location):
    CompanyCommanderUDP.main(company_num,location)


def gui_thread():
    App = QApplication(sys.argv)
    aw = ApplicationWindow()
    aw.show()
    update_field_thread = threading.Thread(target=aw.update_field)
    update_field_thread.start()
    sys.exit(App.exec_())


def main(company_num,location):
    cc_thread = threading.Thread(target=company_commander_thread,args=(company_num,location))
    gui_thread1 = threading.Thread(target=gui_thread)

    cc_thread.start()
    gui_thread1.start()

公司指挥官线程运行UDP套接字,GUI线程运行基于PyQt5的GUI。

因为GUI本身在线程(gui_thread)中运行,所以我发现很难从closeEvent中关闭它。 有什么方法或想法我该怎么做才能停止closeEvent中的所有线程?

suleo123 回答:closeEvent并不能真正关闭整个应用程序

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3157896.html

大家都在问