Skip to content Skip to sidebar Skip to footer

Pyqt5 The Qlcnumber Doesnt Update

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

Solution 1:

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

Post a Comment for "Pyqt5 The Qlcnumber Doesnt Update"