以下最小代码在 QThread 的 run for 循环中崩溃。这在删除小部件对象时有效。
import sys
import time
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Widget(QLabel):
def __init__(self):
super(Widget, self).__init__()
self.setText("hello")
self.show()
class Worker(QThread):
def __init__(self):
super(Worker, self).__init__()
def run(self):
for i in range(1,2):
label = QLabel()
label.setText(str(i))
label.show()
time.sleep(1)
label.close()
app = QApplication(sys.argv)
widget = Widget()
worker = Worker()
worker.start()
sys.exit(app.exec_())