环境:windows Python 3.6.5 非常简单的一个 qt 应用,代码如下
from PyQt5 import QtCore
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(985, 635)
class MyWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setupUi(self)
def test(self,b):
print(b)
app = QApplication(sys.argv)
myWin = MyWindow()
class MyThead(QtCore.QThread):
updated = QtCore.pyqtSignal(str)
def __init__(self, msg):
super().__init__()
self.msg = msg
def run(self):
self.updated.emit(self.msg)
#--------------后半部分
_thread = MyThead('123')
_thread.updated.connect(myWin.test)
_thread.start()
myWin.show()
app.exec()
运行起来没问题
但是如果把后面几行
_thread = MyThead('123')
_thread.updated.connect(myWin.test)
_thread.start()
放到方法里面,也就是代码后半部分变成
def _():
_thread = MyThead('123')
_thread.updated.connect(myWin.test)
_thread.start()
_()
myWin.show()
app.exec()
gui 直接崩溃,也没有报错,只有一个因异常导致的 QThread: Destroyed while thread is still running
这里折腾一天了才排查到这个地方,文档也没找到相关内容...
1
just1 OP 0 回复惨案...
|
2
zone53 2018-08-20 18:36:12 +08:00 via iPhone 1
出了_()这个函数,线程对象不就释放了么。
|