实现效果:可以显示一张图片,该图片可以居中显示,且在本身图片比例保持不变的情况下,随窗口的变化而缩放。
写作目的:因为网上没有找到pyqt的比较简洁相关例子,所以我仿造参考的qt流程写了一份,希望对你有帮助。
一 先决条件
- python环境
- pyqt5
- 会用Qt designer
- 会把ui文件转py
- 知道怎么用Qlabel显示一张图片
二 主要代码
import sys
from PyQt5 import QtGui
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtGui import QPixmap
from ui.img_read import Ui_MainWindow
class QHelloWidget(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self.pixmap = QPixmap("img/2.png") # 按指定路径找到图片
def showEvent(self, a0: QtGui.QShowEvent) -> None:
self.label.setPixmap(self.pixmap.scaled(self.label.size(), aspectRatioMode=Qt.KeepAspectRatio)) # 在label上显示图片
def resizeEvent(self, a0: QtGui.QResizeEvent) -> None:
self.label.setPixmap(self.pixmap.scaled(self.label.size(), aspectRatioMode=Qt.KeepAspectRatio)) # 在label上显示图片
if __name__ == '__main__':
app = QApplication(sys.argv)
helloWidget = QHelloWidget()
helloWidget.show()
sys.exit(app.exec_())
代码解释:
- 关键在于pixmap.scaled命令,需要传入label的大小,和传入参数Qt.KeepAspectRatio,作用是保证图片等比例显示。
-
showEvent:窗口生成的时候会调用此函数
-
resizeEvent:窗口大小改变的时候会调用此函数
三 需要在Qt designer中做一些设置
1.调整你定义的相关Qlabel标签中的sizePolicy
将水平策略和垂直策略都改为Ignored
2.调整你定义的相关Qlabel标签中的alignment
将水平的和垂直的改为AlignHCenter和AlignVCenter
别忘了把更改过的设置同步到py文件上
三 完整代码
1.ui文件
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'img_read.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(700, 510)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Ignored)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
self.label.setSizePolicy(sizePolicy)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 700, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "TextLabel"))
2.main_window
import sys
from PyQt5 import QtGui
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtGui import QPixmap
from ui.img_read import Ui_MainWindow
class QHelloWidget(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self.pixmap = QPixmap("img/2.png") # 按指定路径找到图片
def showEvent(self, a0: QtGui.QShowEvent) -> None:
self.label.setPixmap(self.pixmap.scaled(self.label.size(), aspectRatioMode=Qt.KeepAspectRatio)) # 在label上显示图片
def resizeEvent(self, a0: QtGui.QResizeEvent) -> None:
self.label.setPixmap(self.pixmap.scaled(self.label.size(), aspectRatioMode=Qt.KeepAspectRatio)) # 在label上显示图片
if __name__ == '__main__':
app = QApplication(sys.argv)
helloWidget = QHelloWidget()
helloWidget.show()
sys.exit(app.exec_())