在写一个处理大数据量excel时候,开启子线程处理数据和开启弹窗展示进度的时候导致程序完全退出,没有任何报错,而且在
pycharm中debug可以,run不行
查看了网上的答案后发现,其实是字段作用域的问题,一开始窗口和子线程均未加self. 导致方法执行结束后线程和窗口均被回收,程序退出,需要将其设为主窗口对象的属性即可
触发处理代码如下
def showSuccess(self,message):
self.loading.close()
QMessageBox.information(self, "提示",
'处理成功 ^_^',
QMessageBox.Yes | QMessageBox.No)
def showError(self,message):
self.loading.close()
QMessageBox.warning(self, "失败", "处理失败啦。" + message, QMessageBox.Yes | QMessageBox.No)
def process(self):
self.loading = LoadingPop()
self.loading.show()
self.thread_1 = Work(inputFilePath, outpath, num)
self.thread_1.messageTxtValue.connect(self.loading.set_message)
self.thread_1.showSuccess.connect(self.showSuccess)
self.thread_1.showError.connect(self.showError)
self.thread_1.start()
弹窗代码也很简单,design画的 两个labelText,一个填充转圈的gif,一个展示信息
class LoadingPop(QWidget, Ui_LoadingForm):
def __init__(self):
super(LoadingPop, self).__init__()
self.setupUi(self)
jpg = QtGui.QPixmap("images/loading.gif")
self.imageLabel.setPixmap(jpg)
def set_message(self, message):
self.textLabel.setText(message)
子线程主要是处理excel数据
class Work(QThread):
messageTxtValue = pyqtSignal(str)
showSuccess = pyqtSignal(str)
showError = pyqtSignal(str)
def __init__(self, inputFilePath, outpath, num):
super(Work, self).__init__()
self.inputFilePath = inputFilePath
self.outpath = outpath
self.num = num
def run(self):
self.messageTxtValue.emit('开始读取excel文件')
try:
tool = DiffAssignTool(self.inputFilePath, self.outpath, self.num, self.messageTxtValue)
tool.create_file()
self.showSuccess.emit('成功啦')
except BaseException as e:
self.showError.emit(str(e))
两个成功失败的信号槽是为了能弹窗展示信息,并关闭进度窗口