1 回答
TA贡献1796条经验 获得超10个赞
尝试一下:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
self.label = QLabel()
self.pixmap = QPixmap("head.jpg")
self.label.setPixmap(self.pixmap.scaled(self.label.size(),
Qt.KeepAspectRatio, Qt.SmoothTransformation))
self.label.setSizePolicy(QSizePolicy.Expanding,
QSizePolicy.Expanding)
self.label.setAlignment(Qt.AlignCenter)
self.label.setMinimumSize(100, 100)
layout = QGridLayout(centralWidget)
layout.addWidget(self.label)
def resizeEvent(self, event):
scaledSize = self.label.size()
scaledSize.scale(self.label.size(), Qt.KeepAspectRatio)
if not self.label.pixmap() or scaledSize != self.label.pixmap().size():
self.updateLabel()
def updateLabel(self):
self.label.setPixmap(self.pixmap.scaled(
self.label.size(), Qt.KeepAspectRatio,
Qt.SmoothTransformation))
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
添加回答
举报