PyQt5 QLCNumber不更新

2024-09-24 08:31:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个计算脉冲的程序,然后它通过一些等式,并在gui中显示出来。 这是我的主菜

import sys
import time
import RPi.GPIO as GPIO
import PyQt5
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from mainwindow import Ui_MainWindow
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)
 
class MainWindow(QMainWindow):
 # access variables inside of the UI's file
 def __init__(self):
     super().__init__()
     self.mainwindow = Ui_MainWindow()
     self.mainwindow.setupUi(self)
     self.i=100
     self.flow = 0
     self.flowliter = 0
     self.totalflow=0

     
     self.mainwindow.lcdNumber.display(self.i)
     self.mainwindow.lcdNumber_2.display(self.i)  
     self.show()
     
 
     self.mainwindow.startbttn.clicked.connect(lambda: self.pressedstartButton())
     self.mainwindow.stopbttn.clicked.connect(lambda: self.pressedstopButton())
     
 
 def pressedstartButton(self):
     print ("Pressed On!")
     self.data()
 
 def pressedstopButton(self):
     print ("Pressed Off!")
     
 def data(self) :
     global count
     count = 0

     def countPulse(channel):
      global count
      if start_counter == 1:
          count = count+1

     GPIO.add_event_detect(FLOW_SENSOR, GPIO.FALLING, callback=countPulse)

     while True:
        try:
         start_counter = 1
         time.sleep(1)
         start_counter = 4

         self.flow = (10 * 60)
         self.flowliter= (self.flow/60)
         self.totalflow += self.flowliter
         print("%d"% (count))
         print ("The flow is: %.3f Liter/min" % (self.flow))
         print ("The flowliter is: %.3f Liter" % (self.flowliter))
         print ("The volume is: %.3f Liter" % (self.totalflow))
         self.mainwindow.lcdNumber.display(self.flow)
         self.mainwindow.lcdNumber_2.display(self.flowliter)
         count = 0
         time.sleep(1)
        except KeyboardInterrupt:
         print ('\ncaught keyboard interrupt!, bye')
         GPIO.cleanup()
         sys.exit()


def main():
     app = QApplication(sys.argv)
     form = MainWindow()
     form.show()
     sys.exit(app.exec_())
 
if __name__ == "__main__":
    main()

lcdnumber不会在gui中更新,但会在shell中更新self.flow,我希望显示该值 在gui中,但我不知道哪一个适合qtablewidget或qtextbroswer 此代码应计算来自gpio 18的脉冲,并在gui中显示流程


Tags: importselfgpiotimedefcountdisplaysys
1条回答
网友
1楼 · 发布于 2024-09-24 08:31:19

您不应该使用耗时的函数,因为它们会阻塞eventloop,结果是冻结GUI。在这种情况下,您不应该使用无限循环或time.sleep,但一个QTimer就足够了

import sys
import RPi.GPIO as GPIO

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

from mainwindow import Ui_MainWindow


FLOW_SENSOR = 18


class MainWindow(QMainWindow):
    pinSignal = pyqtSignal()

    def __init__(self):
        super().__init__()
        self.mainwindow = Ui_MainWindow()
        self.mainwindow.setupUi(self)
        self.i = 100
        self.flow = 0
        self.flowliter = 0
        self.totalflow = 0

        self.mainwindow.lcdNumber.display(self.i)
        self.mainwindow.lcdNumber_2.display(self.i)
        self.show()

        self.mainwindow.startbttn.clicked.connect(self.pressedstartButton)
        self.mainwindow.stopbttn.clicked.connect(self.pressedstopButton)

        self.start_counter = False
        self.count = 0

        self.timer = QTimer(self, interval=1000, timeout=self.execute_every_second)
        self.pinSignal.connect(self.handle_pin_signal)

    def pressedstartButton(self):
        print("Pressed On!")
        GPIO.add_event_detect(FLOW_SENSOR, GPIO.FALLING, callback = lambda *args: self.pinSignal.emit())
        self.execute_every_second()
        self.timer.start()

    def pressedstopButton(self):
        print("Pressed Off!")
        self.timer.stop()
        GPIO.remove_event_detect(FLOW_SENSOR)

    def handle_pin_signal(self):
        if self.start_counter:
            self.count += 1

    def execute_every_second(self):
        if not self.start_counter:
            self.flow = 10 * 60
            self.flowliter = self.flow / 60
            self.totalflow += self.flowliter
            print("%d" % (self.count))
            print("The flow is: %.3f Liter/min" % (self.flow))
            print("The flowliter is: %.3f Liter" % (self.flowliter))
            print("The volume is: %.3f Liter" % (self.totalflow))
            self.mainwindow.lcdNumber.display(self.flow)
            self.mainwindow.lcdNumber_2.display(self.flowliter)
            self.count = 0
        self.start_counter = not self.start_counter


def main():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(FLOW_SENSOR, GPIO.IN, pull_up_down=GPIO.PUD_UP)

    app = QApplication(sys.argv)
    form = MainWindow()
    form.show()
    ret = app.exec_()

    GPIO.cleanup()

    sys.exit(ret)


if __name__ == "__main__":
    main()

相关问题 更多 >