如何将我的 LCD 与类测试连接以显示 x?

我正在尝试创建一个 GUI 来显示从 Raspberry 获取的机器数据。


不幸的是,我无法更新我的 QT-Desinger 表面。


所以我现在在这个“测试课”上尝试,但遗憾的是没有成功


那是我已经拥有的。有些东西不见了……但我现在不知道什么


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()

计数器显示 0 并且循环根本没有开始..


我的目标是让 LCD 不断更新自己,这可能吗?


Helenr
浏览 156回答 1
1回答

慕桂英4014372

对于更新 x 的值,QTimer 是使用 PyQt 时最好的方法,您不需要使用 threading 模块from PyQt5.Qt import QLCDNumber, QDialog, QPushButton, QVBoxLayout, QApplication,QTimerimport sysclass LCD(QDialog):&nbsp; &nbsp; x = 0&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; super(LCD, self).__init__()&nbsp; &nbsp; &nbsp; &nbsp; self.lcdNumber = QLCDNumber()&nbsp; &nbsp; &nbsp; &nbsp; self.pushStart = QPushButton("Start")&nbsp; &nbsp; &nbsp; &nbsp; self.pushStart.clicked.connect(self.update)&nbsp; &nbsp; &nbsp; &nbsp; vBox = QVBoxLayout()&nbsp; &nbsp; &nbsp; &nbsp; vBox.addWidget(self.lcdNumber)&nbsp; &nbsp; &nbsp; &nbsp; vBox.addWidget(self.pushStart)&nbsp; &nbsp; &nbsp; &nbsp; self.setLayout(vBox)&nbsp; &nbsp; &nbsp; &nbsp; self.timer = QTimer()&nbsp; &nbsp; &nbsp; &nbsp; self.timer.timeout.connect(self.update)&nbsp; &nbsp; def update(self):&nbsp; &nbsp; &nbsp; &nbsp; self.lcdNumber.display(str(self.x))&nbsp; &nbsp; &nbsp; &nbsp; self.x += 1&nbsp; &nbsp; &nbsp; &nbsp; self.timer.start(1000)if __name__ == "__main__":&nbsp; &nbsp; app = QApplication(sys.argv)&nbsp; &nbsp; lcd = LCD()&nbsp; &nbsp; lcd.show()&nbsp; &nbsp; sys.exit(app.exec_())
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python