首页 > 解决方案 > PYQT5:防止图像标签改变窗口大小

问题描述

我正在创建一个简单的图像查看器,它能够使用左/右箭头键循环浏览图像列表。问题是当序列中出现更大的图像时,窗口大小也会变大。我希望图像变小以适应窗口大小,而不是相反。另外,我想让它在用户调整窗口大小时改变它的大小(尽管永远不会大于它的原始大小)。我将如何去做这些?

import sys
import os
import time

from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore as qtc

class MainWindow(qtw.QMainWindow):
    def __init__(self):
        super().__init__()
        self.image_label = qtw.QLabel(alignment=qtc.Qt.AlignCenter)
        self.image_handler()
        self.curr_img = self.images[0]
        self.curr_img_pixmap = qtg.QPixmap(os.path.join(r'C:\Desktop\Python\images', self.curr_img))
        self.resize_image(0)
        self.image_label.setPixmap(self.curr_img_pixmap)
        self.viewer()
        self.setCentralWidget(self.viewer_widget)
        self.setStyleSheet("""* {
            background-color: #0d0d0d;
        }
        .QLabel {
            border-style: none;
        }
        .QMainWindow {
            border-style: none;
        }
        """)
        self.show()

    def image_handler(self):
        self.images = [image for image in os.listdir(r'C:\Desktop\Python\images')]

    def resize_image(self, mode=0):
        if mode == 0:
            img_width = self.curr_img_pixmap.width()
            img_height = self.curr_img_pixmap.height()
            if img_width > self.width():
                self.curr_img_pixmap = self.curr_img_pixmap.scaledToWidth(self.width())
            elif img_height > self.height():
                self.curr_img_pixmap = self.curr_img_pixmap.scaledToHeight(self.height())
            else:
                self.curr_img_pixmap = self.curr_img_pixmap.scaledToHeight(self.height())

    def viewer(self):
        self.viewer_widget = qtw.QMainWindow()
        self.viewer_widget.setCentralWidget(self.image_label)

    def change_image(self, direction):
        current_index = self.images.index(self.curr_img)
        if direction == qtc.Qt.Key_Left:
            if current_index == 0:
                return
            self.curr_img = self.images[current_index-1]
            self.curr_img_pixmap = qtg.QPixmap(os.path.join(r'C:\Desktop\Python\images', self.curr_img))
            self.resize_image(0)
            self.image_label.setPixmap(self.curr_img_pixmap)
        if direction == qtc.Qt.Key_Right:
            print(current_index)
            if current_index == len(self.images)-1:
                return
            self.curr_img = self.images[current_index+1]
            self.curr_img_pixmap = qtg.QPixmap(os.path.join(r'C:\Desktop\Python\images', self.curr_img))
            self.resize_image(0)
            self.image_label.setPixmap(self.curr_img_pixmap)

    def keyPressEvent(self, event):
        if event.key() == qtc.Qt.Key_Left:
            self.change_image(qtc.Qt.Key_Left)
        if event.key() == qtc.Qt.Key_Right:
            self.change_image(qtc.Qt.Key_Right)

    def resizeEvent(self, event):
        self.resize_image(0)
        self.image_label.setPixmap(self.curr_img_pixmap)

if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    mw = MainWindow()
    sys.exit(app.exec_())

PS:这里的代码不在python中。

标签: pythonpyqt5

解决方案


使用 .scaled 方法与 keepAspectRatio 以及 QLabel 的宽度和高度

import sys
import os
import time
from PyQt5 import Qt
from PyQt5.QtCore import *
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore as qtc

class MainWindow(qtw.QMainWindow):
    def __init__(self):
        super().__init__()
        self.image_label = qtw.QLabel(alignment=qtc.Qt.AlignCenter)
        self.image_label.setMinimumSize(1, 1)
        self.image_handler()
        self.curr_img = self.images[0]
        self.curr_img_pixmap = qtg.QPixmap(os.path.join(r'C:\Desktop\Python\images', self.curr_img))
        self.image_label.setPixmap(self.curr_img_pixmap.scaled(self.image_label.width(), self.image_label.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation))
        self.viewer()
        self.setCentralWidget(self.viewer_widget)
        self.setStyleSheet("""* {
            background-color: #0d0d0d;
        }
        .QLabel {
            border-style: none;
        }
        .QMainWindow {
            border-style: none;
        }
        """)
        self.show()

    def image_handler(self):
        self.images = [image for image in os.listdir(r'C:\Desktop\Python\images')]

    def viewer(self):
        self.viewer_widget = qtw.QMainWindow()
        self.viewer_widget.setCentralWidget(self.image_label)

    def change_image(self, direction):
        current_index = self.images.index(self.curr_img)
        if direction == qtc.Qt.Key_Left:
            if current_index == 0:
                return
            self.curr_img = self.images[current_index-1]
            self.curr_img_pixmap = qtg.QPixmap(os.path.join(r'C:\Desktop\Python\images', self.curr_img))
            self.image_label.setPixmap(self.curr_img_pixmap.scaled(self.image_label.width(), self.image_label.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation))
        if direction == qtc.Qt.Key_Right:
            if current_index == len(self.images)-1:
                return
            self.curr_img = self.images[current_index+1]
            self.curr_img_pixmap = qtg.QPixmap(os.path.join(r'C:\Desktop\Python\images', self.curr_img))
            self.image_label.setPixmap(self.curr_img_pixmap.scaled(self.image_label.width(), self.image_label.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation))

    def keyPressEvent(self, event):
        if event.key() == qtc.Qt.Key_Left:
            self.change_image(qtc.Qt.Key_Left)
        if event.key() == qtc.Qt.Key_Right:
            self.change_image(qtc.Qt.Key_Right)

    def resizeEvent(self, event):
        #self.resize_image(0)
        print(event)
        self.image_label.setPixmap(self.curr_img_pixmap.scaled(self.image_label.width(), self.image_label.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation))

if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    mw = MainWindow()
    sys.exit(app.exec_())

推荐阅读