【发布时间】:2021-06-16 05:18:11
【问题描述】:
我正在创建一个简单的图像查看器,它能够使用左/右箭头键循环浏览图像列表。问题是当序列中出现更大的图像时,窗口大小也会变大。我希望图像变小以适应窗口大小,而不是相反。另外,我想让它在用户调整窗口大小时改变它的大小(尽管永远不会大于它的原始大小)。我将如何去做这些?
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_())
P.S:代码here不在python中。
【问题讨论】:
-
查看我对相关帖子的回答:Resizing a window with PyQT5 - how do I reduce the size of a widget to allow the window to be shrunk?(不是 OP 提供的解决方案)。这个概念是 QLabel 不允许调整到比为其设置的像素图更小的尺寸(只有更大的尺寸是可能的),所以最简单的解决方案是使用自定义小部件。
-
@musicamante 我无法评论你的其他帖子,所以我在这里问这个问题:你说使用 QScrollArea 是个坏主意,但如果我想提供放大/缩小功能对于图像, QScrollArea 不是理想的吗?或者有其他方法吗?
-
IF 您还需要缩放功能,那么情况显然非常不同,QScrollArea 显然 可能 有意义,但随后出现了其他问题:如果调整窗口大小会发生什么?它应该保持图像比例和窗口大小之间的当前比率吗?还是应该尝试使“当前规模”适应新的规模?比例尺应该根据最小尺寸(宽度或高度)还是最大尺寸进行调整?