2

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.

  • Qt does not support gui operations of any kind outside the main thread. Use `QThread` and send a signal back to the main thread with the message you want to show. There are many examples showing how to do this on SO and elsewhere. – ekhumoro Jan 29 '19 at 19:38
  • `while True` might be re-creating the `QmessageBox` over and over again. Have a look at pyqt threading tutorials how to use threads with QT correctly. – Maurice Meyer Jan 29 '19 at 19:40

0 Answers0