0

I'm trying to make my app support multi threading in connection with GUI , I'm trying to connect to method inside GUI from threading outside GUI, I have inspired this idea from Simplest way for PyQT Threading and it was marked as working solution , where is my fault

Below is the error.

The Error

class Communicate(QtCore.QObject):
    myGUI_signal = QtCore.pyqtSignal(str)

def myThread(callbackFunc):
# Setup the signal-slot mechanism.
    mySrc = Communicate()
    mySrc.myGUI_signal.connect(callbackFunc)

# Endless loop. You typically want the thread
# to run forever.
    while(True):
    # Do something useful here.
        msgForGui = 'This is a message to send to the GUI'
        mySrc.myGUI_signal.emit(msgForGui)


FORM_CLASS, _ = loadUiType(os.path.join(os.path.dirname('__file__'), "main.ui"))

class MainApp(QMainWindow, FORM_CLASS):  # QMainWindow refere to window type used in ui file
# this is constructor
    def __init__(self, parent=None):
        super(MainApp, self).__init__(parent)
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.ui()
        self.actions()

    def ui(self):
        self.setFixedSize(848, 663)
    
    def actions(self):
        self.pushButton.clicked.connect(self.startTheThread)

    def theCallbackFunc(self, msg):
        print('the thread has sent this message to the GUI:')
        print(msg)
        print('---------')

    def startTheThread(self):
    # Create the new thread. The target function is 'myThread'. The
    # function we created in the beginning.
        t = threading.Thread(name = 'myThread', target = myThread, args =(self.theCallbackFunc))
        t.start()

def main():
    app = QApplication(sys.argv)
    window = MainApp()  # calling class of main window (first window)
    window.show()  # to show window
    app.exec_()  # infinit loop to make continous show for window

if __name__ == '__main__':
    main()
Mahendra Gunawardena
  • 1,956
  • 5
  • 26
  • 45

1 Answers1

0

When instantiating the Python thread, args must be a tuple or list (or other iterable). args =(self.theCallbackFunc) is not a tuple. args =(self.theCallbackFunc,) is a tuple (note the extra comma needed for tuples containing a single value).

three_pineapples
  • 11,579
  • 5
  • 38
  • 75