PyQt5 The QLCNumber doesnt update(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 ('
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()
这篇关于PyQt5 QLCNumber 没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PyQt5 QLCNumber 没有更新
基础教程推荐
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 筛选NumPy数组 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01