代码之家  ›  专栏  ›  技术社区  ›  abdelrahman

具有PyQt5多线程的Python GUI

  •  0
  • abdelrahman  · 技术社区  · 6 年前

    我试图让我的应用程序支持GUI中的多线程,我试图从GUI外的线程连接到GUI内的方法,我从 Simplest way for PyQT Threading 它被标记为有效解决方案,我的错在哪里

    下面是错误。

    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()
    
    1 回复  |  直到 4 年前
        1
  •  0
  •   three_pineapples    6 年前

    在实例化Python线程时, args 必须是元组或列表(或其他iterable)。 args =(self.theCallbackFunc) 不是元组。 args =(self.theCallbackFunc,) 是一个元组(请注意,包含单个值的元组需要额外的逗号)。