您当前的位置:首页 > 计算机 > 编程开发 > Python

pyqt5 --弹出图片预览框

时间:08-12来源:作者:点击数:

pyqt5 --弹出图片预览框

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QDialog, QHBoxLayout
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt


class ImageDialog(QDialog):
    def __init__(self, image_path):
        super().__init__()
        self.setWindowTitle('Image Dialog')

        layout = QVBoxLayout()

        image_label = QLabel()
        pixmap = QPixmap(image_path)
        image_label.setPixmap(pixmap)
        image_label.setAlignment(Qt.AlignCenter)

        layout.addWidget(image_label)
        self.setLayout(layout)


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Main Window')
        self.setGeometry(100, 100, 300, 200)

        button = QPushButton('Show Image', self)
        button.clicked.connect(self.show_image_dialog)

        layout = QVBoxLayout()
        layout.addWidget(button)
        self.setLayout(layout)

    def show_image_dialog(self):
        image_path = r'D:\code\fenxi\static\shoukuanma_simple.png'  # 替换为你的图片路径
        dialog = ImageDialog(image_path)
        dialog.exec_()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐