现在通过阅读和提问我已经达到了可以在液晶显示器上显示数值的程度,但现在想制作一个图表。
在下面的图片中,我将有一张图片作为图表的背景。对于Dyno,收集的重要信息是Torque和HorsePower。这些我是用我的Python 3.5代码计算的,数据是通过使用arduino来收集的。
对于我的图表,我实际上想要在数据进入的同时绘制两条线。我想同时绘制Torque和HorsePower。这两个与使用Dyno的时间相比。然而,这可能很难,因为它们需要用不同的y轴绘制。从我一直在阅读使用pyqtGraph是这项工作的最佳选择,但由于我从事这类工作的经验,我真的不知道该怎么做。
以下是我尝试根据我找到的一些内容运行的代码。运行它不会导致我的代码出错,但它也不会与图形区域交互。我尝试以与液晶显示器工作方式类似的方式使其工作,但即使我仍然没有任何工作。
"""
SCSU DYNO GUI PROGRAM
created 10/20/2017
"""
import sys
import time
import csv
import numpy as np
import warnings
import serial
import serial.tools.list_ports
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QThread,QTimer, pyqtSignal
from PyQt5.QtWidgets import QMessageBox,QWidget, QApplication
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import random
from DynoTest1 import Ui_DynoTest1
__author__ = 'Matt Munn'
class GetData(QThread):
dataChanged = pyqtSignal(float, float, float, float, float, float, float, float)
#Distance = 0.5 #This is dependent on the lever arm.
def __init__(self, parent=None):
QThread.__init__(self, parent)
arduino_ports = [ # automatically searches for an Arduino and selects the port it's on
p.device
for p in serial.tools.list_ports.comports()
if 'Arduino' in p.description
]
if not arduino_ports:
raise IOError("No Arduino found - is it plugged in? If so, restart computer.")
if len(arduino_ports) > 1:
warnings.warn('Multiple Arduinos found - using the first')
self.Arduino = serial.Serial(arduino_ports[0], 9600, timeout=1)
def __del__(self): # part of the standard format of a QThread
self.wait()
def run(self): # also a required QThread function, the working part
self.Arduino.close()
self.Arduino.open()
self.Arduino.flush()
self.Arduino.reset_input_buffer()
start_time = time.time()
Distance = 0.5 #This is dependent on the lever arm.
Max_RPM = 0
Max_HorsePower = 0
Max_Torque = 0
while True:
while self.Arduino.inWaiting() == 0:
pass
try:
data = self.Arduino.readline()
dataarray = data.decode().rstrip().split(',')
self.Arduino.reset_input_buffer()
Force = round(float(dataarray[0]), 3)
RPM = round(float(dataarray[1]), 3)
if Max_RPM < RPM:
Max_RPM = RPM
Torque = round(Force * Distance, 3)
if Max_Torque < Torque:
Max_Torque = Torque
HorsePower = round(Torque * RPM / 5252, 3)
if Max_HorsePower < HorsePower:
Max_HorsePower = HorsePower
Run_Time = round(time.time() - start_time, 3)
print(Force, 'Grams', ",", RPM, 'RPMs', ",", Torque, "ft-lbs", ",", HorsePower, "hp", Run_Time,
"Time Elasped")
self.dataChanged.emit(Force, RPM, Max_RPM, Torque, Max_Torque, HorsePower, Max_HorsePower, Run_Time)
except (KeyboardInterrupt, SystemExit, IndexError, ValueError):
pass
class GUI(QWidget, Ui_DynoTest1):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setupUi(self)
self.thread = GetData(self)
self.thread.dataChanged.connect(self.onDataChanged)
self.thread.start()
"""
layout = QtGui.QHBoxLayout()
self.plot = pg.PlotWidget()
layout.addWidget(self.plot)
self.setLayout(layout)
def plotter(self):
self.data = [0]
self.curve = self.plot.getPlotItem().plot()
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updater)
self.timer.start(0)
def updater(self):
self.data.append(self.data[-1]+0.2*(0.5-random.random()))
self.curve.setData(self.data)
"""
def onDataChanged(self, Force, RPM, Max_RPM, Torque,Max_Torque, HorsePower, Max_HorsePower, Run_Time):
self.lcdNumber.display(Max_RPM)
self.lcdNumber_2.display(Max_Torque)
self.lcdNumber_3.display(Max_HorsePower)
self.lcdNumber_4.display(RPM)
self.lcdNumber_5.display(Torque)
self.lcdNumber_6.display(HorsePower)
self.lcdNumber_7.display(Run_Time)
#self.graphicsView.display(Tourque,Run_Time)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
Dyno = GUI()
Dyno.show()
sys.exit(app.exec_())
这是QTDesigner生成的代码。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'dynotest1.ui'
#
# Created by: PyQt5 UI code generator 5.9
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_DynoTest1(object):
def setupUi(self, DynoTest1):
DynoTest1.setObjectName("DynoTest1")
DynoTest1.resize(1001, 695)
self.verticalLayout_4 = QtWidgets.QVBoxLayout(DynoTest1)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout_3 = QtWidgets.QVBoxLayout()
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.pushButton_2 = QtWidgets.QPushButton(DynoTest1)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 1, 0, 1, 1)
self.pushButton_4 = QtWidgets.QPushButton(DynoTest1)
self.pushButton_4.setObjectName("pushButton_4")
self.gridLayout.addWidget(self.pushButton_4, 1, 1, 1, 1)
self.pushButton_3 = QtWidgets.QPushButton(DynoTest1)
self.pushButton_3.setObjectName("pushButton_3")
self.gridLayout.addWidget(self.pushButton_3, 0, 1, 1, 1)
self.pushButton = QtWidgets.QPushButton(DynoTest1)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
self.verticalLayout_3.addLayout(self.gridLayout)
self.label_3 = QtWidgets.QLabel(DynoTest1)
self.label_3.setObjectName("label_3")
self.verticalLayout_3.addWidget(self.label_3)
self.label_2 = QtWidgets.QLabel(DynoTest1)
self.label_2.setObjectName("label_2")
self.verticalLayout_3.addWidget(self.label_2)
self.label = QtWidgets.QLabel(DynoTest1)
self.label.setObjectName("label")
self.verticalLayout_3.addWidget(self.label)
self.horizontalLayout.addLayout(self.verticalLayout_3)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.label_5 = QtWidgets.QLabel(DynoTest1)
self.label_5.setObjectName("label_5")
self.verticalLayout.addWidget(self.label_5)
self.lcdNumber_4 = QtWidgets.QLCDNumber(DynoTest1)
self.lcdNumber_4.setFrameShape(QtWidgets.QFrame.Box)
self.lcdNumber_4.setFrameShadow(QtWidgets.QFrame.Raised)
self.lcdNumber_4.setLineWidth(1)
self.lcdNumber_4.setSmallDecimalPoint(True)
self.lcdNumber_4.setDigitCount(5)
self.lcdNumber_4.setMode(QtWidgets.QLCDNumber.Dec)
self.lcdNumber_4.setSegmentStyle(QtWidgets.QLCDNumber.Filled)
self.lcdNumber_4.setProperty("value", 0.0)
self.lcdNumber_4.setObjectName("lcdNumber_4")
self.verticalLayout.addWidget(self.lcdNumber_4)
self.lcdNumber_5 = QtWidgets.QLCDNumber(DynoTest1)
font = QtGui.QFont()
font.setBold(False)
font.setWeight(50)
self.lcdNumber_5.setFont(font)
self.lcdNumber_5.setSmallDecimalPoint(True)
self.lcdNumber_5.setObjectName("lcdNumber_5")
self.verticalLayout.addWidget(self.lcdNumber_5)
self.lcdNumber_6 = QtWidgets.QLCDNumber(DynoTest1)
self.lcdNumber_6.setSmallDecimalPoint(True)
self.lcdNumber_6.setObjectName("lcdNumber_6")
self.verticalLayout.addWidget(self.lcdNumber_6)
self.horizontalLayout.addLayout(self.verticalLayout)
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.label_6 = QtWidgets.QLabel(DynoTest1)
self.label_6.setObjectName("label_6")
self.verticalLayout_2.addWidget(self.label_6)
self.lcdNumber = QtWidgets.QLCDNumber(DynoTest1)
self.lcdNumber.setSmallDecimalPoint(True)
self.lcdNumber.setObjectName("lcdNumber")
self.verticalLayout_2.addWidget(self.lcdNumber)
self.lcdNumber_2 = QtWidgets.QLCDNumber(DynoTest1)
self.lcdNumber_2.setSmallDecimalPoint(True)
self.lcdNumber_2.setObjectName("lcdNumber_2")
self.verticalLayout_2.addWidget(self.lcdNumber_2)
self.lcdNumber_3 = QtWidgets.QLCDNumber(DynoTest1)
self.lcdNumber_3.setSmallDecimalPoint(True)
self.lcdNumber_3.setObjectName("lcdNumber_3")
self.verticalLayout_2.addWidget(self.lcdNumber_3)
self.horizontalLayout.addLayout(self.verticalLayout_2)
self.horizontalLayout_2.addLayout(self.horizontalLayout)
self.verticalLayout_5 = QtWidgets.QVBoxLayout()
self.verticalLayout_5.setObjectName("verticalLayout_5")
self.label_7 = QtWidgets.QLabel(DynoTest1)
self.label_7.setObjectName("label_7")
self.verticalLayout_5.addWidget(self.label_7)
self.lcdNumber_7 = QtWidgets.QLCDNumber(DynoTest1)
self.lcdNumber_7.setObjectName("lcdNumber_7")
self.verticalLayout_5.addWidget(self.lcdNumber_7)
self.horizontalLayout_2.addLayout(self.verticalLayout_5)
self.verticalLayout_4.addLayout(self.horizontalLayout_2)
self.graphicsView = QtWidgets.QGraphicsView(DynoTest1)
self.graphicsView.setStyleSheet("border-image: url(:/newPrefix/husky_head5.png);")
self.graphicsView.setLineWidth(1)
self.graphicsView.setObjectName("graphicsView")
self.verticalLayout_4.addWidget(self.graphicsView)
self.retranslateUi(DynoTest1)
QtCore.QMetaObject.connectSlotsByName(DynoTest1)
def retranslateUi(self, DynoTest1):
_translate = QtCore.QCoreApplication.translate
DynoTest1.setWindowTitle(_translate("DynoTest1", "DynoTest1"))
self.pushButton_2.setText(_translate("DynoTest1", "Pause"))
self.pushButton_4.setText(_translate("DynoTest1", "Print"))
self.pushButton_3.setText(_translate("DynoTest1", "Stop"))
self.pushButton.setText(_translate("DynoTest1", "Start"))
self.label_3.setText(_translate("DynoTest1", "<html><head/><body><p align=\"center\"><span style=\" font-size:18pt; font-weight:600;\">RPMs</span></p></body></html>"))
self.label_2.setText(_translate("DynoTest1", "<html><head/><body><p align=\"center\"><span style=\" font-size:18pt; font-weight:600;\">Torque (ft-lbs)</span></p></body></html>"))
self.label.setText(_translate("DynoTest1", "<html><head/><body><p align=\"center\"><span style=\" font-size:18pt; font-weight:600;\">Horse Power</span></p></body></html>"))
self.label_5.setText(_translate("DynoTest1", "<html><head/><body><p align=\"center\"><span style=\" font-size:18pt; font-weight:600;\">Now</span></p></body></html>"))
self.label_6.setText(_translate("DynoTest1", "<html><head/><body><p align=\"center\"><span style=\" font-size:18pt; font-weight:600;\">Max</span></p></body></html>"))
self.label_7.setText(_translate("DynoTest1", "<html><head/><body><p align=\"center\"><span style=\" font-size:18pt; font-weight:600;\">Run Time</span></p><p align=\"center\"><span style=\" font-size:18pt; font-weight:600;\">(Seconds)</span></p></body></html>"))
import Resource_rc
GUI的图片
答案 0 :(得分:0)
首先是将PlotWidget添加到GUI。为此我们使用QGraphicsView(实际上可以使用任何小部件),在QGraphicsView中我们添加一个布局,在这个布局中我们添加了PlotWidget。
$clientSecretData = file_get_contents('../../private_files/client_secret.json');
然后创建项目,并且如果您希望有2个轴并且在同一个图中,我们将以下link中的代码作为示例。
在您想要使用的示例中,使用计时器更新数据,但在这种情况下,每次调用onDataChanged方法时,时钟必须为其读取数据。要获得视图,已创建一个过程,仅存储和显示最后50个元素。
<强>代码:强>
layout = QHBoxLayout()
self.plot = pg.PlotWidget()
layout.addWidget(self.plot)
self.graphicsView.setLayout(layout)