pyqt点击右上角关闭界面但是子线程仍在运行

现象:

通过右上角的叉关闭图形界面后,程序运行的子线程却不会被自动关闭,依然留存在系统中
原因:

子线程没有正确关闭
解决方法:

1.将子线程设置成守护线程

self.your_thread = threading.Thread(target=self.tcp_client_concurrency)
# 设置线程为守护线程,防止退出主线程时,子线程仍在运行
self.your_thread.setDaemon(True)
# 新线程启动
self.your_thread.start()

2.重构 def closeEvent(self, event): 函数

def closeEvent(self, event):
"""
对MainWindow的函数closeEvent进行重构
退出软件时结束所有进程
:param event:
:return:
"""
reply = QtWidgets.QMessageBox.question(self,
'本程序',
"是否要退出程序?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
QtWidgets.QMessageBox.No)
if reply == QtWidgets.QMessageBox.Yes:
event.accept()
os._exit(0)
else:
event.ignore()

转载自:https://blog.csdn.net/wbdxz/article/details/86292393

参考资料:

https://blog.csdn.net/qingwufeiyang12346/article/details/78314370
https://ask.csdn.net/questions/701749
https://blog.csdn.net/weixin_42192419/article/details/80843088

原文地址:https://www.cnblogs.com/foreverlin/p/10881346.html