Search code examples
pythonmultithreadingpyqt5lcd

how is it possible to connect my LCD with the class test so that x is displayed?


I'm trying to create a GUI that shows machine data taken from a Raspberry.

Unfortunately, I can't get my QT-Desinger surface updated.

So Im trying it now on this "test class" but sadly not successful

That ist was i allready have. Something is missing... but i dont now what

x = 0
class Ui_Form(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.lcdNumber = QtWidgets.QLCDNumber(Form)
        self.lcdNumber.setGeometry(QtCore.QRect(10, 50, 361, 191))
        self.lcdNumber.setObjectName("lcdNumber")
        self.lcdNumber.display(x)
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))

    def run(self):
        if __name__ == "__main__":
            app = QtWidgets.QApplication(sys.argv)
            Form = QtWidgets.QWidget()
            ui = Ui_Form()
            ui.setupUi(Form)
            Form.show()
            sys.exit(app.exec_())

class Test(threading.Thread):

    global x
    def __init__(self):
        threading.Thread.__init__(self)
    def runs(self):
        while x <= 20:
            print(x)
            x = x + 1
            time.sleep(2)

t = Ui_Form()
t1 = Test()

t.start()
t1.start()

the counter shows 0 and the loop dont start at all..

my goal was for the LCD to constantly update itself an schow x is that possible ?

thanks


Solution

  • For update the value of x, QTimer is the best way when using PyQt, you do not need to use the threading module

    from PyQt5.Qt import QLCDNumber, QDialog, QPushButton, QVBoxLayout, QApplication,QTimer
    import sys
    
    class LCD(QDialog):
        x = 0
        def __init__(self):
            super(LCD, self).__init__()
    
            self.lcdNumber = QLCDNumber()
            self.pushStart = QPushButton("Start")
            self.pushStart.clicked.connect(self.update)
    
            vBox = QVBoxLayout()
            vBox.addWidget(self.lcdNumber)
            vBox.addWidget(self.pushStart)
    
            self.setLayout(vBox)
    
            self.timer = QTimer()
            self.timer.timeout.connect(self.update)
    
        def update(self):
            self.lcdNumber.display(str(self.x))
            self.x += 1
            self.timer.start(1000)
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        lcd = LCD()
        lcd.show()
        sys.exit(app.exec_())