2017-08-24 673 views
0

首先,我希望你们有一定的耐心,因为我是这些项目的新手,我也希望不要问愚蠢的问题。话虽如此,我的主要目标是为树莓派3创建一个用户界面,它将感应来自电池和太阳能电池板的电压,电流等。将pyqtgraph图嵌入QT .ui?

因为我正在研究树莓,并对Python3有一些知识,所以我决定使用QTCreator,据我所知可以通过pyqt(https://nikolak.com/pyqt-qt-designer-getting-started/)将其翻译成python3。我将它安装在我的树莓派上,并制作了以下用户界面:

有了基本的用户界面之后,我将.ui文件转换成.py与pyuic5命令,我可以用“python3 main”打开UI。 PY”,一切似乎是正确的:

的UI看起来如何打开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_() 

是否可以将该图嵌入到我的main.py文件中?如果我理解正确,我应该使用QTCreator上的推广小部件功能。在此先感谢你们!

回答

2

什么是通过Qt设计推广是一个小部件,即一类,所以它不能被直接推动,我们必须做的是将它放在一个类中,如下图所示:

Plotter.py

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

在进行之前,我会假设文件具有以下结构:

. 
├── main.py 
└── Plotter.py 
  1. 做的第一件事是选择小部件:

enter image description here

  • 然后我们右键点击这个并选择选项促进..
  • enter image description here

  • 在对话框中我们把CustomWidget促进类名Plotter.h头文件,然后按添加促进按钮。
  • enter image description here

  • 然后我们把我们的.ui文件。PY
  • enter image description here

    +0

    非常感谢eyllanesc,你的答案是非常有用的!我现在可以将我的情节嵌入到我在QTCreator中制作的UI中!我只是有一些额外的疑问,希望你能帮助我: 1)在你的第三步,你写道:“在对话框中,我们把CustomPlot放在推广类名中。如果我的理解正确,名称应该是CustomWidget,因为我们应该将类的名称放在我们的Plotter.py文件中,对吧? (这就是你在图片上写的东西,也许你有点混淆名字) –

    +0

    在Promoted class Name中你放置了制作图形的类的名字,在我的例子中是CustomWidget,并且在你放置的Header File中将扩展名.py更改为.h的文件名称 – eyllanesc

    +0

    您只能升级类,因此请将脚本修改为类。如果你有一些疑虑,你可以通过Dropbox,驱动器或类似的文件共享你的文件,我会为你做的,也不要忘了标记我的答案是正确的 – eyllanesc