用Qt designer创建一个Gui文件,添加push button、GroupBox、Widget、Horizontal Slider控件,并将Widget放在GroupBox里面
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(881, 687)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(550, 640, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.groupBox = QtWidgets.QGroupBox(Dialog)
self.groupBox.setGeometry(QtCore.QRect(280, 0, 601, 551))
self.groupBox.setObjectName("groupBox")
self.widget = QtWidgets.QWidget(self.groupBox)
self.widget.setGeometry(QtCore.QRect(60, 90, 471, 341))
self.widget.setObjectName("widget")
self.horizontalSlider = QtWidgets.QSlider(self.groupBox)
self.horizontalSlider.setGeometry(QtCore.QRect(230, 440, 160, 22))
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(70, 80, 93, 28))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.groupBox.setTitle(_translate("Dialog", "GroupBox"))
self.pushButton.setText(_translate("Dialog", "加载nii图像"))
另创一个py文件 用于导入matplotlib以及创建和使用FigureCanvas和Figure类
import matplotlib
matplotlib.use("Qt5Agg") # 声明使用QT5
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class MyFigure(FigureCanvas):
def __init__(self,width=5, height=4, dpi=100):
#创建一个创建Figure
self.fig = Figure(figsize=(width, height), dpi=dpi)
#在父类中激活Figure窗口
super(MyFigure,self).__init__(self.fig) #此句必不可少,否则不能显示图形
#创建一个子图,用于绘制图形用,111表示子图编号,如matlab的subplot(1,1,1)
self.axes = self.fig.add_subplot(111)
from PyQt5.QtWidgets import *
import sys
from MyDialog import Ui_Dialog#导入GUI文件
from MyFigure import *#嵌入了matplotlib的文件
from pathlib import Path
import nibabel as nib
class MainDialogImgBW(QDialog,Ui_Dialog):
def __init__(self):
super(MainDialogImgBW,self).__init__()
self.setupUi(self)
self.setWindowTitle("显示nii图像")
self.setMinimumSize(0,0)
#创建存放nii文件路径的属性
self.nii_path=''
#创建记录nii文件里面图片数量的属性
self.shape=1
#定义MyFigure类的一个实例
self.F = MyFigure(width=3, height=2, dpi=100)
#在GUI的groupBox中创建一个布局,用于添加MyFigure类的实例(即图形)后其他部件。
self.gridlayout = QGridLayout(self.groupBox) # 继承容器groupBox
self.gridlayout.addWidget(self.F,0,1)
self.pushButton.clicked.connect(self.bindButton)
self.horizontalSlider.valueChanged.connect(self.bindSlider)
def showimage(self,slice_idx):
data_nii = nib.load(Path(self.nii_path))
data1=data_nii.get_fdata()
self.shape = data1.shape[-1]
self.horizontalSlider.setRange(1,data1.shape[-1])
fig = self.F.figure
fig.clear()
ax = fig.add_subplot(111) # 将画布划成1*1的大小并将图像放在1号位置,给画布加上一个坐标轴
ax.imshow(data1[:, :, slice_idx - 1], cmap='gray')
fig.canvas.draw()
def bindSlider(self):
slice_idx = self.horizontalSlider.value()
self.showimage(slice_idx)
def bindButton(self):
file_name = QFileDialog.getOpenFileName(None, "Open File", "./", "nii(*.nii.gz;*.nii)")
self.nii_path = file_name[0]
slice_idx = self.horizontalSlider.value()
self.showimage(slice_idx)
if __name__ == "__main__":
app = QApplication(sys.argv)
main = MainDialogImgBW()
main.show()
sys.exit(app.exec_())