在Qt Designer中先建立UI,转成py文件,文件名为:plot_ui.py
# -*- coding: utf-8 -*- ################################################################################ ## Form generated from reading UI file 'designerfVqEfi.ui' ## ## Created by: Qt User Interface Compiler version 6.3.0 ## ## WARNING! All changes made in this file will be lost when recompiling UI file! ################################################################################ from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale, QMetaObject, QObject, QPoint, QRect, QSize, QTime, QUrl, Qt) from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont, QFontDatabase, QGradient, QIcon, QImage, QKeySequence, QLinearGradient, QPainter, QPalette, QPixmap, QRadialGradient, QTransform) from PySide6.QtWidgets import (QApplication, QFormLayout, QMainWindow, QMenuBar, QPushButton, QSizePolicy, QStatusBar, QWidget) class Ui_MainWindow(object): def setupUi(self, MainWindow): if not MainWindow.objectName(): MainWindow.setObjectName(u"MainWindow") MainWindow.resize(800, 600) self.centralwidget = QWidget(MainWindow) self.centralwidget.setObjectName(u"centralwidget") self.formLayoutWidget = QWidget(self.centralwidget) self.formLayoutWidget.setObjectName(u"formLayoutWidget") self.formLayoutWidget.setGeometry(QRect(50, 30, 381, 201)) self.formLayout = QFormLayout(self.formLayoutWidget) self.formLayout.setObjectName(u"formLayout") self.formLayout.setContentsMargins(0, 0, 0, 0) self.sinbtn = QPushButton(self.centralwidget) self.sinbtn.setObjectName(u"sinbtn") self.sinbtn.setGeometry(QRect(110, 260, 80, 25)) self.cosbtn = QPushButton(self.centralwidget) self.cosbtn.setObjectName(u"cosbtn") self.cosbtn.setGeometry(QRect(260, 260, 80, 25)) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QMenuBar(MainWindow) self.menubar.setObjectName(u"menubar") self.menubar.setGeometry(QRect(0, 0, 800, 22)) MainWindow.setMenuBar(self.menubar) self.statusbar = QStatusBar(MainWindow) self.statusbar.setObjectName(u"statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QMetaObject.connectSlotsByName(MainWindow) # setupUi def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None)) self.sinbtn.setText(QCoreApplication.translate("MainWindow", u"PushButton", None)) self.cosbtn.setText(QCoreApplication.translate("MainWindow", u"PushButton", None)) # retranslateUi
再创建一个文件,使用PySide6来演示Ui
from PySide6.QtWidgets import QApplication, QMainWindow,QWidget import pyqtgraph as pg import sys from PySide6 import QtCore,QtWidgets import numpy as np from plot_ui import Ui_MainWindow class pgplot(QMainWindow): def __init__(self): super().__init__() self.ui = Ui_MainWindow() self.ui.setupUi(self) self.__create_connections() def __create_connections(self): self.ui.sinbtn.clicked.connect(self.__plotsin) self.ui.cosbtn.clicked.connect(self.__plotcos) def __clear_item(self):# 清除图像 item_list = list(range(self.ui.formLayout.count())) for i in item_list: item = self.ui.formLayout.itemAt(i) self.ui.formLayout.removeItem(item) # if item.widget(): # item.widget().deleteLater() def __plotsin(self): self.__clear_item() plt= pg.plot() plt.setBackground('w') pen = pg.mkPen(color='#ff0000',width = 2,style = QtCore.Qt.DashLine) x = np.arange(0,160) y = np.sin(x*0.1) line1 = plt.plot(x,y,pen=pen) self.ui.formLayout.addWidget(plt) def __plotcos(self): self.__clear_item() plt= pg.plot() plt.setBackground('w') pen = pg.mkPen(color='g',width = 2,style = QtCore.Qt.DashDotDotLine) x = np.arange(0,100) z = np.cos(x*0.1) line2 = plt.plot(x,z,pen=pen) self.ui.formLayout.addWidget(plt) if __name__ == '__main__': app = QApplication(sys.argv) w = pgplot() w.show() app.exec()
标签:__,self,pyqtgraph,画图,item,ui,formLayout,QTdesigner,MainWindow From: https://www.cnblogs.com/conpi/p/16774726.html