PyQt显示图像

该文章展示了如何使用Python的PyQt5库创建一个图形用户界面,该界面包含一个QGraphicsView用于显示图片和一个QPushButton。当用户点击按钮时,会弹出QFileDialog让用户选择图像文件,然后在QGraphicsView中显示所选图像。
部署运行你感兴趣的模型镜像

纯代码方式编写:
view_image.py

import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import *


class PyQtGraphicDemo(QWidget):
    def __init__(self, parent=None):
        super(PyQtGraphicDemo, self).__init__(parent)
        self.resize(800, 600)    
        # 显示控件
        self.graphicsView = QGraphicsView(self)
        # 按钮
        self.pushButton = QPushButton(self)
        self.pushButton.setText("PushButton")
        self.pushButton.clicked.connect(self.showImage)
        # 布局
        self.verticalLayout = QVBoxLayout(self)
        self.verticalLayout.addWidget(self.graphicsView)
        self.verticalLayout.addWidget(self.pushButton)
        self.setLayout(self.verticalLayout)

    def showImage(self):
        fileName, filetype = QFileDialog.getOpenFileName(self, "请选择图像:", '.', "Image files (*.bmp *.jpg *.png)")
        if fileName != '':
            self.pixmap = QPixmap(fileName)
            self.pixmapItem = QGraphicsPixmapItem(self.pixmap)
            self.scene = QGraphicsScene()
            self.scene.addItem(self.pixmapItem)
            self.graphicsView.setScene(self.scene)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = PyQtGraphicDemo()
    window.show()
    sys.exit(app.exec_())

借助Qt creator编辑ui界面:
widget.ui(在Qt creator中编辑)

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="QGraphicsView" name="graphicsView"/>
   </item>
   <item>
    <widget class="QPushButton" name="pushButton">
     <property name="text">
      <string>PushButton</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

上述文件可以编译生成python代码如下:
Ui_widget.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'd:\document\VScode_workspace\tree_las\image\widget.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(800, 600)
        self.verticalLayout = QtWidgets.QVBoxLayout(Form)
        self.verticalLayout.setObjectName("verticalLayout")
        self.graphicsView = QtWidgets.QGraphicsView(Form)
        self.graphicsView.setObjectName("graphicsView")
        self.verticalLayout.addWidget(self.graphicsView)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.pushButton)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "PushButton"))

main.py

import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import *
from Ui_widget import Ui_Form


class PyQtGraphicDemo(QWidget, Ui_Form):
    def __init__(self, parent=None):
        super(PyQtGraphicDemo, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.showImage)

    def showImage(self):
        fileName, filetype = QFileDialog.getOpenFileName(self, "请选择图像:", '.', "Image files (*.bmp *.jpg *.png)")
        if fileName != '':
            self.pixmap = QPixmap(fileName)
            self.pixmapItem = QGraphicsPixmapItem(self.pixmap)
            self.scene = QGraphicsScene()
            self.scene.addItem(self.pixmapItem)
            self.graphicsView.setScene(self.scene)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = PyQtGraphicDemo()
    window.show()
    sys.exit(app.exec_())

在这里插入图片描述

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

给算法爸爸上香

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值