Python Forum
Newbie trying to pass text from different thread
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie trying to pass text from different thread
#2
Not sure to understand what you are doing exactly, but here is a threading example in PyQt5.

#!/usr/bin/python3
import time
from PyQt5 import QtWidgets, QtCore


class WorkerThread(QtCore.QObject):
    signalExample = QtCore.pyqtSignal(str, int)

    def __init__(self):
        super().__init__()

    @QtCore.pyqtSlot()
    def run(self):
        while True:  # Long running task ...
            self.signalExample.emit("leet", 1337)
            time.sleep(5)


class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.worker = WorkerThread()
        self.workerThread = QtCore.QThread()  # Move the Worker object to the Thread object
        self.workerThread.started.connect(self.worker.run)  # Init worker run() at startup
        self.worker.moveToThread(self.workerThread)
        self.worker.signalExample.connect(self.signalExample)  # Connect your signals/slots
        self.workerThread.start()

    def signalExample(self, text, number):
        print(text)
        print(number)


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    gui = Main()
    app.exec()
Reply


Messages In This Thread
RE: Newbie trying to pass text from different thread - by Alfalfa - Dec-18-2020, 01:12 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020