After implementing a thread to prevent the main GUI from freezing during the function process, my QmessageBox error messages display the title, but not the text and becomes unresponsive. If the 'X' in the QmessageBox is clicked the main GUI becomes unresponsive as well. I've read in other posts that only the main thread can call GUI functions but I'm still lost as to how to implement that. Any advice? I'm very new to threading; this is the first project I've used it with.
class myMainWindow(QtWidgets.QMainWindow, Ui_L3TServiceDeskAssistant):
processing = False
def __init__(self, parent=None):
#init stuff here
def generateToken_click(self):
if not self.processing:
self.processing = True
threading.Thread(target=self.generateToken_thread).start()
def generateToken_thread(self):
while True:
try:
username = self.txtUsername.text()
password = self.txtPassword.text()
HDusername = self.txtHDEmail.text()
UPN = self.txtUPN.text()
#domain changed for privacy purposes
UPNCheck=['.123.com','.321.com']
if UPN[-8:] in UPNCheck:
pass
else:
self.txtUPN.setText("")
error_msg = QMessageBox()
error_msg.setWindowTitle("User Error")
error_msg.setText('Invalid UPN. Please try again.')
error_msg.exec()
self.processing = False
return False
#other steps in the function here
except:
error_msg = QMessageBox()
error_msg.setWindowTitle("User Error")
error_msg.setText('Error. Please try again.')
error_msg.setDefaultButton(QMessageBox.Ok)
error_msg.exec()
self.txtUPN.setText("")
self.processing = False
return False
Note This is just a snippet of the code. There are multiple steps in the function with error messages specific to those steps. I'm just showing the first one as an example. The main UI is imported from a different .py file.