Background thread with QThread in PyQt

后端 未结 6 1228
忘掉有多难
忘掉有多难 2020-11-22 05:39

I have a program which interfaces with a radio I am using via a gui I wrote in PyQt. Obviously one of the main functions of the radio is to transmit data, but to do this co

6条回答
  •  借酒劲吻你
    2020-11-22 06:32

    Take this answer updated for PyQt5, python 3.4

    Use this as a pattern to start a worker that does not take data and return data as they are available to the form.

    1 - Worker class is made smaller and put in its own file worker.py for easy memorization and independent software reuse.

    2 - The main.py file is the file that defines the GUI Form class

    3 - The thread object is not subclassed.

    4 - Both thread object and the worker object belong to the Form object

    5 - Steps of the procedure are within the comments.

    # worker.py
    from PyQt5.QtCore import QThread, QObject, pyqtSignal, pyqtSlot
    import time
    
    
    class Worker(QObject):
        finished = pyqtSignal()
        intReady = pyqtSignal(int)
    
    
        @pyqtSlot()
        def procCounter(self): # A slot takes no params
            for i in range(1, 100):
                time.sleep(1)
                self.intReady.emit(i)
    
            self.finished.emit()
    

    And the main file is:

      # main.py
      from PyQt5.QtCore import QThread
      from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QGridLayout
      import sys
      import worker
    
    
      class Form(QWidget):
    
        def __init__(self):
           super().__init__()
           self.label = QLabel("0")
    
           # 1 - create Worker and Thread inside the Form
           self.obj = worker.Worker()  # no parent!
           self.thread = QThread()  # no parent!
    
           # 2 - Connect Worker`s Signals to Form method slots to post data.
           self.obj.intReady.connect(self.onIntReady)
    
           # 3 - Move the Worker object to the Thread object
           self.obj.moveToThread(self.thread)
    
           # 4 - Connect Worker Signals to the Thread slots
           self.obj.finished.connect(self.thread.quit)
    
           # 5 - Connect Thread started signal to Worker operational slot method
           self.thread.started.connect(self.obj.procCounter)
    
           # * - Thread finished signal will close the app if you want!
           #self.thread.finished.connect(app.exit)
    
           # 6 - Start the thread
           self.thread.start()
    
           # 7 - Start the form
           self.initUI()
    
    
        def initUI(self):
            grid = QGridLayout()
            self.setLayout(grid)
            grid.addWidget(self.label,0,0)
    
            self.move(300, 150)
            self.setWindowTitle('thread test')
            self.show()
    
        def onIntReady(self, i):
            self.label.setText("{}".format(i))
            #print(i)
    
        app = QApplication(sys.argv)
    
        form = Form()
    
        sys.exit(app.exec_())
    

提交回复
热议问题