问题描述
我想制作一个计算脉冲的程序,然后它会通过一些方程式并将其显示在 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 (' caught 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 中显示流程.
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()