使用PyQT5在QLCDNumber上显示传感器输出

时间:2018-09-12 11:18:56

标签: python-3.x pyqt5 sensor qt-designer qlcdnumber

因此,我开发了一个代码,该代码将检测换能器上的电压,然后输出平均电压,这里的问题是,如何随着传感器值的变化来更新该值,目前它主要用于初始值和然后不进行更新,我使用QTDesigner创建了一个主窗口,并使用Python3和PyQt5库编写代码。

这甚至可能吗?如果可以,怎么办?

这是我的代码:

class ControlMainWindow(QtWidgets.QMainWindow):
    def __init__(self,parent=None):
        super(ControlMainWindow,self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.SettingsButton.clicked.connect(lambda:self.ui.stackedWidget.setCurrentIndex(1))
        self.ui.pushButton_5.clicked.connect(lambda:self.ui.stackedWidget.setCurrentIndex(0))

        self.ui.pushButton.clicked.connect(self.OnSwitch)
        self.ui.pushButton_2.clicked.connect(self.OffSwitch)
        self.ui.DelayButton.clicked.connect(self.Delay_Switch)

    def OnSwitch(self):
        GPIO.output(Relay_1, GPIO.HIGH)

    def OffSwitch(self):
        GPIO.output(Relay_1, GPIO.LOW)

    def Delay_Switch(self):
            time.sleep(5)
            GPIO.output(Relay_1,GPIO.HIGH)



if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mySW = ControlMainWindow()
    mySW.show() 
    sys.exit(app.exec_())  

我已经输入了所有代码,直到调用lcdnumber。

mainwindow类之后的其余代码,我让我的代码控制一个继电器,然后按一下按钮浏览几页,这部分代码运行正常

#header {
    grid-area: hd;
}

2 个答案:

答案 0 :(得分:0)

对不起。我对您使用的某些模块不熟悉。 一般来说,它可能看起来像这样:

main.py

import sys
from random          import randint
from PyQt5.QtWidgets import QApplication, QMainWindow 
from PyQt5.QtCore    import QTimer

from ui import Ui_MainWindow

class Form(QMainWindow):
    def __init__(self):
        super().__init__()

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.i = 0 
        self.voltageMin = 180 
        self.voltageMax = 180
        self.ui.lcdNumberCur.display(self.i)

        self.ui.pushButton.clicked.connect(self.startTimer)

        self.timer = QTimer(self)  
        self.timer.setInterval(1000)
        self.timer.timeout.connect(self.updateData)

        self.show()        

    def startTimer(self):  
        if self.ui.pushButton.text() == "Start Timer":
            self.timer.start(1000) 
            self.ui.pushButton.setText("Stop Timer")            
        else:
            self.ui.pushButton.setText("Start Timer")
            self.timer.stop() 

    def updateData(self):
        voltage = randint(90, 260)                  # <--- insert your average voltage here
        self.ui.lcdNumberCur.display(voltage) 
        if voltage > self.voltageMax:
            self.voltageMax = voltage
            self.ui.lcdNumberMax.display(self.voltageMax) 
        elif voltage < self.voltageMin:
            self.voltageMin = voltage
            self.ui.lcdNumberMin.display(self.voltageMin)            


if __name__ == '__main__':
    app = QApplication(sys.argv)
    frm = Form()
    sys.exit(app.exec_())

ui.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(500, 300)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(0, 0, 400, 200))
        self.groupBox.setObjectName("groupBox")

        self.lcdNumberMax = QtWidgets.QLCDNumber(self.groupBox)
        self.lcdNumberMax.setGeometry(QtCore.QRect(225, 10, 151, 41))
        self.lcdNumberMax.setObjectName("lcdNumberMax")
        self.lcdNumberCur = QtWidgets.QLCDNumber(self.groupBox)
        self.lcdNumberCur.setGeometry(QtCore.QRect(225, 55, 151, 41))
        self.lcdNumberCur.setObjectName("lcdNumberCur")
        self.lcdNumberMin = QtWidgets.QLCDNumber(self.groupBox)
        self.lcdNumberMin.setGeometry(QtCore.QRect(225, 100, 151, 41))
        self.lcdNumberMin.setObjectName("lcdNumberMin")        

        self.label = QtWidgets.QLabel(self.groupBox)
        self.label.setGeometry(QtCore.QRect(20, 50, 150, 50))
        self.label.setObjectName("label")
        self.labelMax = QtWidgets.QLabel(self.groupBox)
        self.labelMax.setGeometry(QtCore.QRect(20, 8, 150, 50))
        self.labelMax.setObjectName("labelMax")
        self.labelMin = QtWidgets.QLabel(self.groupBox)
        self.labelMin.setGeometry(QtCore.QRect(20, 95, 150, 50))
        self.labelMin.setObjectName("labelMin")        

        self.pushButton = QtWidgets.QPushButton("pushButton", self.groupBox) #(self.scrollAreaWidgetContents)
        self.pushButton.setGeometry(QtCore.QRect(180, 165, 75, 23))
        self.pushButton.setObjectName("pushButton")

        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "LCDNumber - Timer(Start-Stop)"))
        self.groupBox.setTitle(_translate("MainWindow", "Data"))
        self.label.setText(_translate("MainWindow", "Current voltage in the network"))
        self.labelMin.setText(_translate("MainWindow", "Min voltage in the network"))
        self.labelMax.setText(_translate("MainWindow", "Max voltage in the network"))
        self.pushButton.setText(_translate("MainWindow", "Start Timer"))

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

enter image description here

答案 1 :(得分:0)

好吧,所以陷入了所有gui东西中,我不得不实现线程化并从gui线程中删除函数。因为问题考虑了传感器的输出,所以我删除了所有与传感器无关的代码,只包括了传感器所需的代码。我希望这是有道理的。

这解决了问题:

class Worker(QtCore.QThread):
    valueFound = QtCore.pyqtSignal(int, name="valueFound")
    GAIN = 2/3
    c1 = 525.4
    c2=28152
    Relay_1 = 27

    def __init__(self, parent=None):
        super(Worker, self).__init__(parent)
        self.runFlag = False
        self.i2c = busio.I2C(board.SCL,board.SDA)
        self.adc = ADS1115(self.i2c)
        GPIO.setmode(GPIO.BCM)        
        GPIO.setup(self.Relay_1, GPIO.OUT)
        GPIO.output(self.Relay_1, GPIO.HIGH)
        GPIO.output(self.Relay_1, GPIO.LOW)

    def startThread(self):
        self.runFlag = True
        self.start()

    def stopThread(self):
        self.runFlag = False



    def run(self):
        while self.runFlag:
            self.valueFound.emit(self.Pressure_Reading())

......

   def run(self):
        while self.runFlag:
            self.valueFound.emit(self.Pressure_Reading())

.....

self.worker = Worker(self)
    self.worker.valueFound.connect(self.OnValueFound)

.....

self.worker.startThread()

    def closeEvent(self, event):
        self.worker.stopThread()
        while self.worker.isRunning():
            pass
        event.accept()

    def OnValueFound(self, value):
        self.ui.lcdNumber.display(value)

添加这些字符使程序可以连续更新LCD编号。