PYQT实现上传图片,保存图片

代码如下 

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

class MyWindow(QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()

        self.setWindowTitle("图片处理")
        self.setGeometry(200, 200, 500, 400)

        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)
        main_layout = QVBoxLayout()  # 整体的垂直布局

        # 上面两个 label 的水平布局
        label_layout = QHBoxLayout()
        self.label1 = QLabel()
        label_layout.addWidget(self.label1)  # 图片 1 居左

        self.label2 = QLabel()
        label_layout.addWidget(self.label2)  # 图片 2 居右
        main_layout.addLayout(label_layout)  # 将水平布局添加到垂直布局

        # 下面两个按钮的水平布局
        button_layout = QHBoxLayout()
        self.button1 = QPushButton("打开图片")
        self.button1.clicked.connect(self.open_image)
        button_layout.addWidget(self.button1)

        self.button2 = QPushButton("复制图片")
        self.button2.clicked.connect(self.copy_image)
        button_layout.addWidget(self.button2)
        main_layout.addLayout(button_layout)  # 将水平布局添加到垂直布局

        self.central_widget.setLayout(main_layout)  # 设置中央部件的布局

    def open_image(self):
        file_dialog = QFileDialog()
        file_path, _ = file_dialog.getOpenFileName(self, "选择图片", "", "Image Files (*.jpg *.png)")
        if file_path:
            pixmap = QPixmap(file_path)
            self.label1.setPixmap(pixmap)

    def copy_image(self):
        pixmap = self.label1.pixmap()
        if pixmap:
            self.label2.setPixmap(pixmap)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

西柚与蓝莓

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

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

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

打赏作者

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

抵扣说明:

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

余额充值