在QTdesigner界面中使用按钮切换pyqtgraph画图
在Qt Designer中先建立UI,转成py文件,文件名为:plot_ui.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | # -*- 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | 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 () |
分类:
Python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具