GUI

时间:2019-06-26 09:17:46

标签: python python-3.x pandas matplotlib pyqt5

我不知道如何在pyqt5 GUI中使用matplotlib。 我试图将matplotlib嵌入到我的GUI应用程序中,但我不在需要在GUI内定义动画的地方。

请不要考虑除sensor-1 QCheckBox和button-1(plot_button)之外的任何其他参数

我定义正确吗?还是我应该做些什么改变?

flag = False
x1 = []
y1 = []
class Ui_MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.numberOfPressed = 0

    def Plot_Button(self,i):
        self.numberOfPressed = self.numberOfPressed + 1
        print("button Pressed. Number : = " + str(self.numberOfPressed))
        lineEdit_11 = self.lineEdit_1.text
        print('Sampling Frequency: ' + self.lineEdit_1.text())
        print('Offset: ' + self.lineEdit_2.text())
        if self.Sensor1.isChecked():
           flag =True                 

    def plot_data(self):
     if(flag == True):
        optimal_frequency = 100
        for i in range(optimal_frequency):
            x = np.arange((2*i),(2+(2*i)))
            y = np.random.rand(2)
            print(x,y)
            self.graphicsView.plot(x,y) 
        else:
            print("nothing")  

    def animate(i):
        self.anim = animation.FuncAnimation(fig, self.plot_data, interval=optimal_frequency)    

    def Pause_Button(self):
        QCoreApplication.instance().quit()
        print("fail")

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(795, 359)     
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")        
        self.Plot = QtWidgets.QPushButton(self.centralwidget)
        self.Plot.setGeometry(QtCore.QRect(530, 260, 80, 23))
        self.Plot.setObjectName("Plot")
        self.Plot.clicked.connect(self.Plot_Button)
        self.Pause = QtWidgets.QPushButton(self.centralwidget)
        self.Pause.setGeometry(QtCore.QRect(620, 260, 80, 23))
        self.Pause.setObjectName("Pause")
        self.Pause.clicked.connect(self.Pause_Button)
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(520, 10, 251, 291))
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")        
        self.label = QtWidgets.QLabel(self.frame)
        self.label.setGeometry(QtCore.QRect(140, 6, 101, 20))
##      self.label.setObjectName("Sampling Frequency")
        self.label.setObjectName("label")              
        self.label_2 = QtWidgets.QLabel(self.frame)
        self.label_2.setGeometry(QtCore.QRect(150, 40, 51, 20))
##        self.label_2.setObjectName("Offset")
        self.label_2.setObjectName("label_2")
##        self.label.setText('Offset:')      
        self.Sensor1 = QtWidgets.QCheckBox(self.frame)
        self.Sensor1.setGeometry(QtCore.QRect(30, 70, 74, 21))
        self.Sensor1.setObjectName("Sensor1")       
        self.Sensor2 = QtWidgets.QCheckBox(self.frame)
        self.Sensor2.setGeometry(QtCore.QRect(30, 90, 74, 21))
        self.Sensor2.setObjectName("Sensor2")      
        self.Sensor3 = QtWidgets.QCheckBox(self.frame)
        self.Sensor3.setGeometry(QtCore.QRect(30, 110, 74, 21))
        self.Sensor3.setObjectName("Sensor3")      
        self.Sensor4 = QtWidgets.QCheckBox(self.frame)
        self.Sensor4.setGeometry(QtCore.QRect(30, 130, 71, 21))
        self.Sensor4.setObjectName("Sensor4")      
        self.Sensor5 = QtWidgets.QCheckBox(self.frame)
        self.Sensor5.setGeometry(QtCore.QRect(30, 150, 74, 21))
        self.Sensor5.setObjectName("Sensor5")      
        self.Sensor6 = QtWidgets.QCheckBox(self.frame)
        self.Sensor6.setGeometry(QtCore.QRect(30, 170, 74, 21))
        self.Sensor6.setObjectName("Sensor6")     
        self.Sensor7 = QtWidgets.QCheckBox(self.frame)
        self.Sensor7.setGeometry(QtCore.QRect(30, 190, 74, 21))
        self.Sensor7.setObjectName("Sensor7")       
        self.Sensor8 = QtWidgets.QCheckBox(self.frame)
        self.Sensor8.setGeometry(QtCore.QRect(30, 210, 71, 21))
        self.Sensor8.setObjectName("Sensor8")
        self.lineEdit_1 = QtWidgets.QLineEdit(self.frame)
        self.lineEdit_1.setGeometry(QtCore.QRect(20, 10, 113, 21))
        self.lineEdit_1.setObjectName("lineEdit_1")        
        self.lineEdit_2 = QtWidgets.QLineEdit(self.frame)
        self.lineEdit_2.setGeometry(QtCore.QRect(20, 40, 113, 20))
        self.lineEdit_2.setObjectName("lineEdit_2")        
##        self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
        self.graphicsView = pg.PlotWidget(self.centralwidget)
        self.graphicsView.setGeometry(QtCore.QRect(5, 11, 501, 291))
        self.graphicsView.setObjectName("graphicsView")       
        self.frame.raise_()
        self.Plot.raise_()
        self.Pause.raise_()
        self.graphicsView.raise_()
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 795, 21))       
        self.menubar.setObjectName("menubar")
        self.menuFile = QtWidgets.QMenu(self.menubar)        
        self.menuFile.setObjectName("menuFile")
        self.menuAbout = QtWidgets.QMenu(self.menubar)        
        self.menuAbout.setObjectName("menuAbout")
        MainWindow.setMenuBar(self.menubar)       
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionOpen = QtWidgets.QAction(MainWindow)
        self.actionOpen.setObjectName("actionOpen")        
        self.actionSave_As = QtWidgets.QAction(MainWindow)
        self.actionSave_As.setObjectName("actionSave_As")        
        self.actionScreenshot = QtWidgets.QAction(MainWindow)
        self.actionScreenshot.setObjectName("actionScreenshot")      
        self.actionExit = QtWidgets.QAction(MainWindow)
        self.actionExit.setObjectName("actionExit")        
        self.menuFile.addAction(self.actionOpen)
        self.menuFile.addAction(self.actionSave_As)
        self.menuFile.addAction(self.actionScreenshot)
        self.menuFile.addAction(self.actionExit)
        self.menubar.addAction(self.menuFile.menuAction())
        self.menubar.addAction(self.menuAbout.menuAction())
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.Plot.setText(_translate("MainWindow", "Plot"))
        self.Pause.setText(_translate("MainWindow", "Pause"))
        self.label.setText(_translate("MainWindow", "Sampling Frequency"))
        self.label_2.setText(_translate("MainWindow", "Offset"))
        self.Sensor1.setText(_translate("MainWindow", "Sensor1"))
        self.Sensor2.setText(_translate("MainWindow", "Sensor2"))
        self.Sensor3.setText(_translate("MainWindow", "Sensor3"))
        self.Sensor4.setText(_translate("MainWindow", "Sensor4"))
        self.Sensor5.setText(_translate("MainWindow", "Sensor5"))
        self.Sensor6.setText(_translate("MainWindow", "Sensor6"))
        self.Sensor7.setText(_translate("MainWindow", "Sensor7"))
        self.Sensor8.setText(_translate("MainWindow", "Sensor8"))
        self.menuFile.setTitle(_translate("MainWindow", "File"))
        self.menuAbout.setTitle(_translate("MainWindow", "About"))
        self.actionOpen.setText(_translate("MainWindow", "Open"))
        self.actionSave_As.setText(_translate("MainWindow", "SavAs"))
        self.actionScreenshot.setText(_translate("MainWindow","Screenshot"))
        self.actionExit.setText(_translate("MainWindow", "Exit"))
        MainWindow.show()

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    qapp = QtWidgets.QApplication(sys.argv)
    sys.exit(app.exec_())

0 个答案:

没有答案