Fab*_*n S 1 python pyqt qt-designer python-3.x pyqtgraph
首先,我希望您有耐心,因为我是这类项目的新手,并且我也希望不要问愚蠢的问题。话虽这么说,我的主要目标是为树莓派3创建一个UI,该UI将从电池和太阳能面板中感应电压,电流等。
由于我正在研究树莓派并且对Python3有所了解,因此我决定使用QTCreator,据我所知,可以通过pyqt将其转换为python3(https://nikolak.com/pyqt-qt-designer-getting-started/)。我将其安装在树莓派上,并创建了以下用户界面:
在拥有基本的用户界面之后,我使用pyuic5命令将.ui文件转换为.py,并且能够使用“ python3 main.py”打开用户界面,一切似乎都正确:
打开main.py文件后用户界面的外观
现在,我想在UI上绘制几张图(例如电压与时间的关系图)。我正在使用以下进行测试:
import sys
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
win = pg.GraphicsWindow()
win.setWindowTitle('pyqtgraph example: Scrolling Plots')
p1 = win.addPlot(labels = {'left':'Voltage', 'bottom':'Time'})
data1 = np.random.normal(size=10)
data2 = np.random.normal(size=10)
curve1 = p1.plot(data1, pen=(3,3))
curve2 = p1.plot(data2, pen=(2,3))
ptr1 = 0
def update1():
global data1, curve1, data2, ptr1
data1[:-1] = data1[1:] # shift data in the array one sample left
# (see also: np.roll)
data1[-1] = np.random.normal()
ptr1 += 1
curve1.setData(data1)
curve1.setPos(ptr1,0)
data2[:-1] = data2[1:] # shift data in the array one sample left
# (see also: np.roll)
data2[-1] = np.random.normal()
curve2.setData(data2)
curve2.setPos(ptr1,0)
def update():
update1()
timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(2000) # number of seconds (every 1000) for next update
if __name__ == '__main__':
QtGui.QApplication.instance().exec_()
Run Code Online (Sandbox Code Playgroud)
是否可以将该图嵌入到我的main.py文件中?如果我理解正确,则应该在QTCreator上使用促进小部件功能。在此先感谢大家!
通过Qt Designer提升的是一个Widget,即一个类,因此无法直接提升它,我们必须做的是将其放置在一个类中,如下所示:
绘图仪
class CustomWidget(pg.GraphicsWindow):
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
ptr1 = 0
def __init__(self, parent=None, **kargs):
pg.GraphicsWindow.__init__(self, **kargs)
self.setParent(parent)
self.setWindowTitle('pyqtgraph example: Scrolling Plots')
p1 = self.addPlot(labels = {'left':'Voltage', 'bottom':'Time'})
self.data1 = np.random.normal(size=10)
self.data2 = np.random.normal(size=10)
self.curve1 = p1.plot(self.data1, pen=(3,3))
self.curve2 = p1.plot(self.data2, pen=(2,3))
timer = pg.QtCore.QTimer(self)
timer.timeout.connect(self.update)
timer.start(2000) # number of seconds (every 1000) for next update
def update(self):
self.data1[:-1] = self.data1[1:] # shift data in the array one sample left
# (see also: np.roll)
self.data1[-1] = np.random.normal()
self.ptr1 += 1
self.curve1.setData(self.data1)
self.curve1.setPos(self.ptr1, 0)
self.data2[:-1] = self.data2[1:] # shift data in the array one sample left
# (see also: np.roll)
self.data2[-1] = np.random.normal()
self.curve2.setData(self.data2)
self.curve2.setPos(self.ptr1,0)
if __name__ == '__main__':
w = CustomWidget()
w.show()
QtGui.QApplication.instance().exec_()
Run Code Online (Sandbox Code Playgroud)
在继续之前,我将假定文件具有以下结构:
.
??? main.py
??? Plotter.py
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4749 次 |
| 最近记录: |