From e11dac82baef61599b7472da59d8fcbdb0442287 Mon Sep 17 00:00:00 2001 From: helldh Date: Wed, 24 Dec 2025 10:53:07 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20oop-exam/image=5Fviewer.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- oop-exam/image_viewer.py | 64 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 oop-exam/image_viewer.py diff --git a/oop-exam/image_viewer.py b/oop-exam/image_viewer.py new file mode 100644 index 0000000..1445771 --- /dev/null +++ b/oop-exam/image_viewer.py @@ -0,0 +1,64 @@ +import sys +from pathlib import Path +from PyQt6.QtWidgets import ( + QApplication, QWidget, QHBoxLayout, QListWidget, QLabel, QFileDialog +) +from PyQt6.QtGui import QPixmap +from PyQt6.QtCore import Qt + +class ImageViewer(QWidget): + def __init__(self): + super().__init__() + self.setWindowTitle("Image Viewer") + self.resize(800, 600) + + self.layout = QHBoxLayout(self) + + # Список файлов + self.file_list = QListWidget() + self.file_list.itemClicked.connect(self.display_image) + self.layout.addWidget(self.file_list, 1) + + # Область для изображения + self.image_label = QLabel("Выберите файл") + self.image_label.setAlignment(Qt.AlignmentFlag.AlignCenter) + self.layout.addWidget(self.image_label, 3) + + # Открыть директорию + self.open_directory() + + def open_directory(self): + dir_path = QFileDialog.getExistingDirectory(self, "Выберите папку с изображениями") + if not dir_path: + return + self.files = list(Path(dir_path).glob("*.[pj][np][gmb]*")) # png/jpg/jpeg/bmp/gif + self.file_list.clear() + for file in self.files: + self.file_list.addItem(file.name) + + def display_image(self, item): + index = self.file_list.row(item) + file_path = self.files[index] + pixmap = QPixmap(str(file_path)) + if pixmap.isNull(): + self.image_label.setText("Не удалось открыть изображение") + return + # Подгонка изображения под размер QLabel с сохранением пропорций + pixmap = pixmap.scaled( + self.image_label.size(), + Qt.AspectRatioMode.KeepAspectRatio, + Qt.TransformationMode.SmoothTransformation + ) + self.image_label.setPixmap(pixmap) + + # Чтобы изображение подгонялось при изменении размера окна + def resizeEvent(self, event): + if self.image_label.pixmap(): + self.display_image(self.file_list.currentItem()) + super().resizeEvent(event) + +if __name__ == "__main__": + app = QApplication(sys.argv) + viewer = ImageViewer() + viewer.show() + sys.exit(app.exec()) \ No newline at end of file