PyQt5 QLCNumber 不更新 [英] PyQt5 The QLCNumber doesnt update

查看:67
本文介绍了PyQt5 QLCNumber 不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个计算脉冲的程序,然后它通过一些方程并将其显示在 gui 中.这是我的 main.py

i want to make a program that count pulses then it go through some equation and display it in the gui . This my main.py

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

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

the lcdnumber doesnt update in the gui but the self.flow update in the shell and i want display the value in the gui but i dont know which one suitable qtablewidget or qtextbroswer this code should count the pulse from the gpio 18 and show the flow in the gui.

推荐答案

您不应该使用耗时的函数,因为它们会阻塞事件循环并导致 GUI 冻结.在这种情况下,您不应该使用无限循环或 time.sleep 但 QTimer 就足够了.

You should not use time-consuming functions as they block the eventloop and the consequence is to freeze the GUI. In this case, you should not use an infinite loop or time.sleep but a QTimer is enough.

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

这篇关于PyQt5 QLCNumber 不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆