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_())
-