我正在使用一个线程来防止GUI在执行操作时冻结,并且我希望它在运行时显示一个弹出窗口,并在完成后自动关闭。如果我在启动后添加消息窗口,则会使程序崩溃。
我研究了线程,找不到类似于我的解决方案。
def showMessageBox(self, title, message):
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(iconpath + "\warning.ico"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
msgBox = QtWidgets.QMessageBox()
msgBox.setIcon(QtWidgets.QMessageBox.Warning)
msgBox.setWindowIcon(icon)
msgBox.setWindowTitle(title)
msgBox.setText(message)
msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok)
msgBox.exec_()
def calculatethread(self, newcase):
try:
os.system('EAZ{0}.EAZ{0}.OUT'.format(newcase))
except:
errorFile = open('{0}\Error.txt'.format(CodeDirectory[-1]), 'a+')
errorFile.write(traceback.format_exc())
errorFile.close()
def calculate(self):
try:
newcase = 'Calculate'
open('{0}.EAZ'.format(newcase), 'w+')
p = threading.Thread(target=self.calculatethread, args=(newcase,))
self.showMessageBox('Loading','Loading...') ## putting this after p.start() crashes and putting this into calculatethread() will crash as well
p.start()
### I understand I could use p.join() and do the below code, but I don't want GUI to freeze, and I was playing with code to see if it would work
except:
errorFile = open('{0}\Error.txt'.format(CodeDirectory[-1]), 'a+')
errorFile.write(traceback.format_exc())
errorFile.close()
我想在不冻结功能的情况下运行该功能,但是有一个弹出窗口告诉用户它仍在工作。谁能指出我在正确的道路上?
答案 0 :(得分:0)
解决了使用信号将工作线程与主线程通信的问题