0

I am writing a GUI application that reads/writes to an external PLC controller through RS232. I used QT Designer (drag and drop widgets program) to create that GUI. At first I have done all the reading and writing process in the main GUI thread, unfortunately, I realized this is the worst way to do so , as the GUI application delays from time to time.

The solution to do so is to use multiThreads - performing all the reading and writing process outside of the Main GUI thread. Unfortunately I have a very basic knowledge about threads or multiThreads. I have been trying to read about it and implementing it , but my implementation was bad and the GUI crashes or didn't show relevant widgets. I would also like to use QTimer instead of While(True) loops.

My code is as follows :



# Main GUI Class:
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(563, 397)
        self.centralwidget = QtWidgets.QWidget(parent=MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(parent=self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(190, 200, 101, 31))
        self.pushButton.setObjectName("pushButton")
        self.label = QtWidgets.QLabel(parent=self.centralwidget)
        self.label.setGeometry(QtCore.QRect(70, 50, 51, 41))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(parent=self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(160, 50, 51, 41))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(parent=self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(250, 50, 51, 41))
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(parent=self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(340, 50, 51, 41))
        self.label_4.setObjectName("label_4")
        self.label_5 = QtWidgets.QLabel(parent=self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(430, 50, 51, 41))
        self.label_5.setObjectName("label_5")
        self.label_6 = QtWidgets.QLabel(parent=self.centralwidget)
        self.label_6.setGeometry(QtCore.QRect(20, 150, 101, 21))
        self.label_6.setObjectName("label_6")
        self.label_7 = QtWidgets.QLabel(parent=self.centralwidget)
        self.label_7.setGeometry(QtCore.QRect(20, 190, 101, 21))
        self.label_7.setObjectName("label_7")
        self.label_8 = QtWidgets.QLabel(parent=self.centralwidget)
        self.label_8.setGeometry(QtCore.QRect(20, 230, 101, 21))
        self.label_8.setObjectName("label_8")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(parent=MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 563, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(parent=MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)


    # RS232 setting up
    def open_port(self):
        self.comlist1 = serial.tools.list_ports.comports()
        self.connected1 = []
        for element in self.comlist1:
            self.connected1.append(element.device + ': ' + element.description)
        self.activeSerialPort1 = self.connected1
        self.omr = serial.Serial(port='COM10', baudrate=9600, timeout=1)
        self.omr.parity = serial.PARITY_NONE
        self.omr.bytesize = 8
        self.omr.stopbits = 1

    
    def Reading_Controller(self):
        
        #Some reading code goes here.

    def Reading_Controller_2(self):

        # Some reading code goes here.

    def Reading_Controller_3(self):

        # Some reading code goes here.


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))
        self.label.setText(_translate("MainWindow", "TextLabel"))
        self.label_2.setText(_translate("MainWindow", "TextLabel"))
        self.label_3.setText(_translate("MainWindow", "TextLabel"))
        self.label_4.setText(_translate("MainWindow", "TextLabel"))
        self.label_5.setText(_translate("MainWindow", "TextLabel"))
        self.label_6.setText(_translate("MainWindow", "TextLabel"))
        self.label_7.setText(_translate("MainWindow", "TextLabel"))
        self.label_8.setText(_translate("MainWindow", "TextLabel"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    ui.open_port()
    MainWindow.show()
    sys.exit(app.exec())



  • Sorry but StackOverflow is not a tutorial website. If you did try to use threading, then provide a valid [mre] based on your attempts, even if they failed. Also be aware that editing pyuic generated files is considered a bad practice (as the header comment in that file, which you ignored and removed, clearly suggests) and you should follow the official guidelines about [using Designer](https://www.riverbankcomputing.com/static/Docs/PyQt6/designer.html) instead. – musicamante Jun 21 '23 at 06:40
  • Thanks on the comment and clarification – עומר דגן Jun 21 '23 at 07:12
  • 1
    I would check out some tutorials on QT and threading, see [this](https://www.pythonguis.com/tutorials/multithreading-pyqt-applications-qthreadpool/) and [this](https://realpython.com/python-pyqt-qthread/) – chrisbeardy Jun 21 '23 at 07:47

0 Answers0