pyqtgraph实时绘图使用在Qt设计器中创建的开始按钮

时间:2018-01-03 03:46:26

标签: python qt plot designer pyqtgraph

请感谢任何帮助!

我在Qt Designer中设计了一个MainWindow,只有一个简单的图形视图小部件和两个按钮:一个用于启动实时信号绘图(例如正弦波),另一个用于停止实时绘图。该文件的名称是“PPGview.ui”。

然后,我在Spyder中创建了PPGview_ui.py文件,我在其中加载了PPGview.ui配置。每当我按下开始按钮时,我想在我的图形视图小部件中更新一个情节,但它会打开另一个窗口并且不会绘制任何东西。

我已经证明了plt.plot(时间,y)和.setdata(时间,y)并且没有

"""
Example: main window
Created on Thu Dec 21 12:07:49 2017
Ref:https://www.youtube.com/watchv=xC9zV3QVqVs&list=PLjARR1053fYm1tNe3lMEqn5acADPxSWTY    
@author: Kevin Machado
"""

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox  
from PyQt5.uic import loadUi
import ctypes
from time import time
import numpy as np
import pyqtgraph as pg


class visualWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        loadUi("PPGview.ui",self)
        self.setWindowTitle('Principal User Interface')
# Window Size propeties
        self.showMaximized()
# Buttons configuration
        self.startButton.clicked.connect(self.startButton1)
# brings a box when closing the UI
        Resolution = ctypes.windll.user32
        Resolution_ancho = Resolution.GetSystemMetrics(0)
        Resolution_alto = Resolution.GetSystemMetrics(1)
        left = (Resolution_ancho / 2) - (self.frameSize().width() / 2)
        top= (Resolution_alto / 2) - (self.frameSize().height() / 2)
        self.move(left, top)
# function when closing        
    def closeEvent(self, event):
        resultado = QMessageBox.question(self, "Salir ...","Seguro desea salir?",
                                         QMessageBox.Yes | QMessageBox.No)
        if resultado == QMessageBox.Yes: event.accept()
        else: event.ignore()
 # function to store the signal
    def getdata(self, tsignal):
        f=10
        sAmpli = 1.0814
        zeroDes = 0.5413

        sR= zeroDes + sAmpli * (0.05*np.sin(2*np.pi*tsignal*3*f)
        + 0.4*np.sin(2*np.pi*tsignal*f) + 0.25*np.sin(2*np.pi*tsignal*2*f+1))

        return sR

    def startButton1(self):
        timestamp= time()
        pw = pg.plot()

        while True:           
            stamp = time() - timestamp
            y = self.getdata(stamp)
            pw.setData(stamp, y, pen=(255,0,0))
            pg.QtGui.QApplication.processEvents()

app = QApplication(sys.argv)
_visualWindow = visualWindow()
_visualWindow.show()
app.exec_()

这是.ui文件:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>794</width>
    <height>533</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <property name="styleSheet">
   <string notr="true">QMainWindow{
background-color: rgb(0,0,0);
}

QPushButton{
background-color: rgb(0,0,0);
color:rgb(176, 176, 0);
font-family: arial;
font-size: 14px;
}</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <property name="styleSheet">
    <string notr="true">QMainWindow{
background-color: rgb(0,0,0);
}
</string>
   </property>
   <widget class="GraphicsLayoutWidget" name="graphicsView">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>50</y>
      <width>771</width>
      <height>381</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="startButton">
    <property name="geometry">
     <rect>
      <x>290</x>
      <y>460</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Start</string>
    </property>
   </widget>
   <widget class="QPushButton" name="stopButton">
    <property name="geometry">
     <rect>
      <x>440</x>
      <y>460</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Stop</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>794</width>
     <height>18</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <customwidgets>
  <customwidget>
   <class>GraphicsLayoutWidget</class>
   <extends>QGraphicsView</extends>
   <header>pyqtgraph</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

0 个答案:

没有答案