pg.PlotWidget图形np.array进入GUI

时间:2018-10-13 08:16:37

标签: python pyqt5

我尝试了不同的解决方案,以将np.array跟踪到pg.PlotWidget跟踪到GUI中。 当我按一下按钮

时不起作用
import sys
import pandas as pd
import re
import numpy as np
import matplotlib.pyplot as plt
import pyqtgraph as pg
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog

class Ui_MainWindow(object):
    def __init__(self):
        self.df = pd.DataFrame()
        self.f_SamplingFreq = 0.0
        self.My_List_Name_Measure = []
        self.str_Name_Measure_Current = ""
        self.index_Measure_Current = 0
        self.List_Data_Raw_Signal = []

    def setupUi(self, MainWindow):
        self.push_Open_Data = QtWidgets.QPushButton(self.centralwidget)
        self.push_Open_Data.setGeometry(QtCore.QRect(10, 10, 80, 21))
        self.push_Open_Data.setStyleSheet("color: rgb(255, 255, 255);\n"
        "background-color: rgb(117, 117, 117);")
        self.push_Open_Data.setObjectName("push_Open_Data")
        self.push_Open_Data.clicked.connect(self.on_click_Open_Data)
        self.graphicsView_SignalSource = pg.PlotWidget(self.centralwidget)
        self.graphicsView_SignalSource.setGeometry(QtCore.QRect(30, 80, 421, 291))
        self.graphicsView_SignalSource.setStyleSheet("background-color: rgb(44, 44, 44);")
        self.graphicsView_SignalSource.setObjectName("graphicsView_SignalSource")


    def on_click_Open_Data(self):
        x = np.array([1,45,8,9])
        y = np.array([1, 4, 9, 16])
        curve = pg.PlotCurveItem(pen='r')
        curve.setData(x)
        self.graphicsView_SignalSource.addItem(curve.getData())
def main():
    app = QtWidgets.QApplication(sys.argv)
    ex = Ui_MainWindow()
    w = QtWidgets.QMainWindow()
    ex.setupUi(w)
    w.show()
    sys.exit(app.exec_())

## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__':
    main()

进程以退出代码-1073740791(0xC0000409)完成

谢谢您的帮助

1 个答案:

答案 0 :(得分:0)

我想您已经消除了诸如Centralwidget之类的元素,并且在您的原始代码中您已经拥有了它。另一方面,潜在的问题是,添加项目时必须添加项目,而不是与项目相关联的数据,因此代码应如下所示:

class Ui_MainWindow(object):
    def __init__(self):
        self.df = pd.DataFrame()
        self.f_SamplingFreq = 0.0
        self.My_List_Name_Measure = []
        self.str_Name_Measure_Current = ""
        self.index_Measure_Current = 0
        self.List_Data_Raw_Signal = []

    def setupUi(self, MainWindow):
        self.centralwidget = QtWidgets.QWidget() # <---
        MainWindow.setCentralWidget(self.centralwidget) # <---
        self.push_Open_Data = QtWidgets.QPushButton(self.centralwidget)
        self.push_Open_Data.setGeometry(QtCore.QRect(10, 10, 80, 21))
        self.push_Open_Data.setStyleSheet("color: rgb(255, 255, 255);\n"
        "background-color: rgb(117, 117, 117);")
        self.push_Open_Data.setObjectName("push_Open_Data")
        self.push_Open_Data.clicked.connect(self.on_click_Open_Data)
        self.graphicsView_SignalSource = pg.PlotWidget(self.centralwidget)
        self.graphicsView_SignalSource.setGeometry(QtCore.QRect(30, 80, 421, 291))
        self.graphicsView_SignalSource.setStyleSheet("background-color: rgb(44, 44, 44);")
        self.graphicsView_SignalSource.setObjectName("graphicsView_SignalSource")

    def on_click_Open_Data(self):
        x = np.array([1,45,8,9])
        y = np.array([1, 4, 9, 16])
        curve = pg.PlotCurveItem(pen='r')
        curve.setData(x)
        self.graphicsView_SignalSource.addItem(curve) # <---

另一方面,我建议您使用布局,以便您的设计可以处理尺寸而不会出现问题,如下所示:

import sys
from PyQt5 import QtCore, QtWidgets
import pyqtgraph as pg
import numpy as np


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)

        button = QtWidgets.QPushButton()
        button.setFixedSize(80, 21)
        button.setStyleSheet("color: rgb(255, 255, 255);\n"
        "background-color: rgb(117, 117, 117);")
        button.clicked.connect(self.on_click_Open_Data)

        self.graphicsView_SignalSource = pg.PlotWidget()

        vlay = QtWidgets.QVBoxLayout(central_widget)
        hlay = QtWidgets.QHBoxLayout()
        hlay.addWidget(button)
        hlay.addStretch()
        vlay.addLayout(hlay)
        vlay.addWidget(self.graphicsView_SignalSource)

    @QtCore.pyqtSlot()
    def on_click_Open_Data(self):
        x = np.array([1,45,8,9])
        y = np.array([1, 4, 9, 16])
        curve = pg.PlotCurveItem(pen='r')
        curve.setData(x)
        self.graphicsView_SignalSource.addItem(curve)

def main():
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

最后,许多IDE无法正确处理错误消息,只指出了它们永远无法帮助的错误代码,例如-1073740791(0xC0000409),在这种情况下,最好的办法是在CMD或控制台中执行脚本,因为他们会为您提供更具描述性的信息。