SeimicPro.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SeismicProgramme</class>
<widget class="QMainWindow" name="SeismicProgramme">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1234</width>
<height>886</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_1" rowstretch="1,12" columnstretch="1,2,2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="horizontalSpacing">
<number>6</number>
</property>
<item row="1" column="2">
<widget class="QGraphicsView" name="distance_view"/>
</item>
<item row="0" column="2">
<widget class="QLabel" name="title_txt_3">
<property name="text">
<string><html><head/><body><p align="center"><span style=" font-size:20pt;">Distance View</span></p></body></html></string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QGraphicsView" name="velocity_view"/>
</item>
<item row="1" column="0">
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="timeLabel">
<property name="text">
<string>time:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="timeLineEdit"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="velocityLabel">
<property name="text">
<string><html><head/><body><p>Velocity:</p></body></html></string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="velocityLineEdit"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="distanceLabel">
<property name="text">
<string>distance:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="distanceLineEdit"/>
</item>
<item row="3" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="4" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="cancel_btn">
<property name="text">
<string>cancel</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="oky_btn">
<property name="text">
<string>ok</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item row="0" column="0">
<widget class="QLabel" name="title_txt">
<property name="text">
<string><html><head/><body><p align="center"><span style=" font-size:20pt;">Seismic Information</span></p></body></html></string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="view_txt">
<property name="text">
<string><html><head/><body><p align="center"><span style=" font-size:20pt;">Velocity View</span></p></body></html></string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
Ui_seismicpro.py
# Form implementation generated from reading ui file 'd:\Cumtb_Code\PyQT\SeismicPro.ui'
#
# Created by: PyQt6 UI code generator 6.1.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_SeismicProgramme(object):
def setupUi(self, SeismicProgramme):
SeismicProgramme.setObjectName("SeismicProgramme")
SeismicProgramme.resize(1234, 886)
self.centralwidget = QtWidgets.QWidget(SeismicProgramme)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.gridLayout_1 = QtWidgets.QGridLayout()
self.gridLayout_1.setContentsMargins(0, 0, 0, -1)
self.gridLayout_1.setHorizontalSpacing(6)
self.gridLayout_1.setObjectName("gridLayout_1")
self.distance_view = QtWidgets.QGraphicsView(self.centralwidget)
self.distance_view.setObjectName("distance_view")
self.gridLayout_1.addWidget(self.distance_view, 1, 2, 1, 1)
self.title_txt_3 = QtWidgets.QLabel(self.centralwidget)
self.title_txt_3.setObjectName("title_txt_3")
self.gridLayout_1.addWidget(self.title_txt_3, 0, 2, 1, 1)
self.velocity_view = QtWidgets.QGraphicsView(self.centralwidget)
self.velocity_view.setObjectName("velocity_view")
self.gridLayout_1.addWidget(self.velocity_view, 1, 1, 1, 1)
self.formLayout = QtWidgets.QFormLayout()
self.formLayout.setObjectName("formLayout")
self.timeLabel = QtWidgets.QLabel(self.centralwidget)
self.timeLabel.setObjectName("timeLabel")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.ItemRole.LabelRole, self.timeLabel)
self.timeLineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.timeLineEdit.setObjectName("timeLineEdit")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.ItemRole.FieldRole, self.timeLineEdit)
self.velocityLabel = QtWidgets.QLabel(self.centralwidget)
self.velocityLabel.setObjectName("velocityLabel")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.ItemRole.LabelRole, self.velocityLabel)
self.velocityLineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.velocityLineEdit.setObjectName("velocityLineEdit")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.ItemRole.FieldRole, self.velocityLineEdit)
self.distanceLabel = QtWidgets.QLabel(self.centralwidget)
self.distanceLabel.setObjectName("distanceLabel")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.ItemRole.LabelRole, self.distanceLabel)
self.distanceLineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.distanceLineEdit.setObjectName("distanceLineEdit")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.ItemRole.FieldRole, self.distanceLineEdit)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)
self.formLayout.setItem(3, QtWidgets.QFormLayout.ItemRole.LabelRole, spacerItem)
spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding)
self.formLayout.setItem(4, QtWidgets.QFormLayout.ItemRole.FieldRole, spacerItem1)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.cancel_btn = QtWidgets.QPushButton(self.centralwidget)
self.cancel_btn.setObjectName("cancel_btn")
self.horizontalLayout.addWidget(self.cancel_btn)
self.oky_btn = QtWidgets.QPushButton(self.centralwidget)
self.oky_btn.setObjectName("oky_btn")
self.horizontalLayout.addWidget(self.oky_btn)
self.formLayout.setLayout(3, QtWidgets.QFormLayout.ItemRole.FieldRole, self.horizontalLayout)
self.gridLayout_1.addLayout(self.formLayout, 1, 0, 1, 1)
self.title_txt = QtWidgets.QLabel(self.centralwidget)
self.title_txt.setObjectName("title_txt")
self.gridLayout_1.addWidget(self.title_txt, 0, 0, 1, 1)
self.view_txt = QtWidgets.QLabel(self.centralwidget)
self.view_txt.setObjectName("view_txt")
self.gridLayout_1.addWidget(self.view_txt, 0, 1, 1, 1)
self.gridLayout_1.setColumnStretch(0, 1)
self.gridLayout_1.setColumnStretch(1, 2)
self.gridLayout_1.setColumnStretch(2, 2)
self.gridLayout_1.setRowStretch(0, 1)
self.gridLayout_1.setRowStretch(1, 12)
self.gridLayout.addLayout(self.gridLayout_1, 0, 0, 1, 1)
SeismicProgramme.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(SeismicProgramme)
self.statusbar.setObjectName("statusbar")
SeismicProgramme.setStatusBar(self.statusbar)
self.retranslateUi(SeismicProgramme)
QtCore.QMetaObject.connectSlotsByName(SeismicProgramme)
def retranslateUi(self, SeismicProgramme):
_translate = QtCore.QCoreApplication.translate
SeismicProgramme.setWindowTitle(_translate("SeismicProgramme", "MainWindow"))
self.title_txt_3.setText(_translate("SeismicProgramme", "<html><head/><body><p align=\"center\"><span style=\" font-size:20pt;\">Distance View</span></p></body></html>"))
self.timeLabel.setText(_translate("SeismicProgramme", "time:"))
self.velocityLabel.setText(_translate("SeismicProgramme", "<html><head/><body><p>Velocity:</p></body></html>"))
self.distanceLabel.setText(_translate("SeismicProgramme", "distance:"))
self.cancel_btn.setText(_translate("SeismicProgramme", "cancel"))
self.oky_btn.setText(_translate("SeismicProgramme", "ok"))
self.title_txt.setText(_translate("SeismicProgramme", "<html><head/><body><p align=\"center\"><span style=\" font-size:20pt;\">Seismic Information</span></p></body></html>"))
self.view_txt.setText(_translate("SeismicProgramme", "<html><head/><body><p align=\"center\"><span style=\" font-size:20pt;\">Velocity View</span></p></body></html>"))
Seismicpro.py
'''
Description: henggao_note
version: v1.0.0
Date: 2023-06-19 20:34:33
LastEditors: henggao
LastEditTime: 2023-06-25 10:00:55
'''
import sys
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_qtagg import FigureCanvas
from matplotlib.figure import Figure
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtCore import Qt
from PyQt6.QtGui import (QBrush, QColor, QImage, QPainter, QPainterPath, QPen,
QPixmap)
from PyQt6.QtWidgets import (QApplication, QDialog, QGraphicsPixmapItem,
QGraphicsRectItem, QGraphicsScene, QGraphicsView)
from Ui_SeismicPro import Ui_SeismicProgramme
class Figure_Canvas(FigureCanvas):
# 通过继承FigureCanvas类,使得该类既是一个PyQt5的Qwidget,又是一个matplotlib的FigureCanvas,这是连接pyqt5与matplot lib的关键
def __init__(self, parent=None, width=11, height=5, dpi=100):
# 创建一个Figure,注意:该Figure为matplotlib下的figure,不是matplotlib.pyplot下面的figure
fig = Figure(figsize=(width, height), dpi=100)
FigureCanvas.__init__(self, fig) # 初始化父类
self.setParent(parent)
# 调用figure下面的add_subplot方法,类似于matplotlib.pyplot下面的subplot方法
self.axes = fig.add_subplot(111)
def test(self):
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [23, 21, 32, 13, 3, 132, 13, 3, 1]
self.axes.plot(x, y)
class SeisPro(QtWidgets.QMainWindow, Ui_SeismicProgramme):
def __init__(self, parent=None):
super(SeisPro, self).__init__(parent)
# 设置窗体
self.setupUi(self)
# 输入框默认值设置
self.timeLineEdit.setText('0')
# self.timeLineEdit.setPlainText("0")
self.velocityLineEdit.setText("0")
self.distanceLineEdit.setText("0")
# 绘图控件,创建一个QGraphicsScene
self.velocity_view.setScene(QGraphicsScene())
# 按钮
self.oky_btn.clicked.connect(self.oky_btn_func)
self.cancel_btn.clicked.connect(self.cancel_btn_fnc)
def oky_btn_func(self): # 查询数据
self.distanceLineEdit.setText("1")
self.add_velocitypic()
self.add_distancepic()
def contour_fnc(self, x, y): # 绘制等值线函数
return (1 - y**5 + x**5) * np.exp(-x**2 - y**2)
def add_velocitypic(self): # 加载速度图形
# 清屏
plt.cla()
# 获取绘图并绘制
fig = plt.figure()
dx = 0.01
dy = 0.01
x = np.arange(-2.0, 2.0, dx)
y = np.arange(-2.0, 2.0, dy)
X, Y = np.meshgrid(x, y)
C = plt.contour(X, Y, self.contour_fnc(X, Y), 12, colors='black')
plt.contourf(X, Y, self.contour_fnc(X, Y), 12, cmap=plt.cm.hot)
plt.clabel(C, inline=1, fontsize=10)
plt.colorbar()
cavans = FigureCanvas(fig)
# 将绘制好的图像设置为中心 Widget
# self.setCentralWidget(cavans)
scene = QGraphicsScene()
scene.addWidget(cavans)
self.velocity_view.setScene(scene)
def add_distancepic(self): # 加载DIstance图形
# 1. 创建场景,创建一个QGraphicsScene,因为加载的图形(FigureCanvas)不能直接放到graphicview控件中,必须先放到graphicScene,然后再把graphicscene放到graphicview中
scene = QGraphicsScene()
# 2. 创建元素
dr = Figure_Canvas()
# 实例化一个FigureCanvas
dr.test() # 画图
# 3.把图形放到QGraphicsScene中,注意:图形是作为一个QWidget放到QGraphicsScene中的
scene.addWidget(dr)
# 4. 将场景添加至视图
self.distance_view.setScene(scene)
def cancel_btn_fnc(self): # 退出按钮
print("退出程序")
sys.exit()
def main():
app = QApplication(sys.argv)
pr = SeisPro()
pr.show()
app.exec()
if __name__ == '__main__':
main()