简体   繁体   中英

Send signals from multiple worker threads to the GUI thread

I have a gui with 24 text boxes, and I'd like to create a thread for each one and update the text box with information from it's respective thread.

Where I'm stuck is receiving the signal to update the gui from all threads.

Code:

#!/usr/bin/python
# Standard Lib
import logging
import os
import sys
import time
# Third Party
from PyQt4 import QtGui
from PyQt4 import QtCore
# Local Kung Fu
from bin import serial_lib, logger, get_args, utils
from bin.assets.test_suite_gui_form import Ui_MainWindow

class Tester(QtCore.QThread):
    def __init__(self):
        QtCore.QThread.__init__(self)
        self.color = "RED"
        self.status = "Disconnected"

    def __del__(self):
        self.wait()

    def run(self):
        self.emit(QtCore.SIGNAL('update(QString)'), "color={} status={}".format(self.color, self.status))
        return

class TestSuiteGUI(QtGui.QMainWindow):

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        # Init class from template
        self.ui = Ui_MainWindow()
        # Get list of com ports to populate app on init
        self.com_ports_list = serial_lib.get_com_ports()
        # Build UI
        self.ui.setupUi(self)
        # Update Labels with COM Ports, where possible, filtered by drivers
        self.update_comm_fields(self.com_ports_list)
        for num, com_port_chunk in zip(range(1,25), self.com_ports_list):
            tester_thread = Tester(com_port_chunk, num)
            tester_thread.start()

    def update_comm_fields(self, com_ports_list):
        for num, port, in zip(range(1, 25), range(0, 24)):
            label = getattr(self.ui, 'com_{}'.format(num))
            label.setText("COM Port: {}".format(com_ports_list[port]["COM"]))

if __name__ == "__main__":
    # Grab args from CLI if necessary
    args = get_args.get_args()
    # Log file
    log_file = os.path.join(utils.app_path, "log", "log.txt")
    # Get Logger
    logger.get_logger(log_file, verbose=True)
    # Init App and display
    app = QtGui.QApplication(sys.argv)
    test_suite = TestSuiteGUI()
    test_suite.show()
    # Close app only when window is closed.
    sys.exit(app.exec_())

Is this the correct approach? I tried using QRunnable and a thread-pool but read somewhere that signals don't work with it. Should I just try python's multithreading library as a last resort, or perhaps an event-based system, since all I need passed is a string and booleans?

You should define and emit the signal like this:

class Tester(QtCore.QThread):
    updateText = QtCore.pyqtSignal(str)
    ...    
    def run(self):
        self.updateText.emit("color={} status={}".format(self.color, self.status))

Then you can connect to the signals like this:

class TestSuiteGUI(QtGui.QMainWindow):
    def __init__(self, parent=None):
        ...
        # keep a reference to the threads
        self._threads = []
        for num, com_port_chunk in zip(range(1,25), self.com_ports_list):
            tester_thread = Tester(com_port_chunk, num)
            # get a reference to the associated textbox somehow...
            textbox = get_the_textbox()
            tester_thread.updateText.connect(textbox.setText)
            tester_thread.start()
            self._threads.append(tester_thread)

Obviously, this is a little sketchy, because it's impossible to actually test your example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM