python pyside6图片切换工具

工具效果如下:

导入图片文件夹,点击上一张,下一张切换

ui界面的代码如下
ui_ImageChange.py代码

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

################################################################################
## Form generated from reading UI file 'ImageChange.ui'
##
## Created by: Qt User Interface Compiler version 6.6.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QLabel, QPushButton, QSizePolicy,
    QWidget)

class Ui_Form(object):
    def setupUi(self, Form):
        if not Form.objectName():
            Form.setObjectName(u"Form")
        Form.resize(564, 603)
        self.shiyanlabel = QLabel(Form)
        self.shiyanlabel.setObjectName(u"shiyanlabel")
        self.shiyanlabel.setGeometry(QRect(20, 60, 531, 461))
        self.shiyanlabel.setAlignment(Qt.AlignCenter)
        self.shiyanButton = QPushButton(Form)
        self.shiyanButton.setObjectName(u"shiyanButton")
        self.shiyanButton.setGeometry(QRect(430, 10, 111, 31))
        self.nextButton = QPushButton(Form)
        self.nextButton.setObjectName(u"nextButton")
        self.nextButton.setGeometry(QRect(440, 550, 121, 41))
        self.preButton = QPushButton(Form)
        self.preButton.setObjectName(u"preButton")
        self.preButton.setGeometry(QRect(10, 550, 121, 41))

        self.retranslateUi(Form)

        QMetaObject.connectSlotsByName(Form)
    # setupUi

    def retranslateUi(self, Form):
        Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
        self.shiyanlabel.setText("")
        self.shiyanButton.setText(QCoreApplication.translate("Form", u"\u5bfc\u5165\u56fe\u7247\u6587\u4ef6\u5939", None))
        self.nextButton.setText(QCoreApplication.translate("Form", u"\u4e0b\u4e00\u5f20", None))
        self.preButton.setText(QCoreApplication.translate("Form", u"\u4e0a\u4e00\u5f20", None))
    # retranslateUi

主要逻辑代码

import os
from PySide6.QtWidgets import QApplication, QWidget, QFileDialog, QMessageBox
from PIL import Image,ImageFilter,ImageQt
from PySide6.QtCore import Qt
from ui_ImageChange import Ui_Form

class myWindow(QWidget, Ui_Form):
    def __init__(self):
        super().__init__()

        # 禁用放大
        self.setWindowFlags(Qt.WindowMinimizeButtonHint | Qt.WindowCloseButtonHint)

        self.curr_img = 0
        self.img_list = []
        self.setupUi(self)

        self.bind()


    def bind(self):
        self.shiyanButton.clicked.connect(self.getImg_shiyan)
        self.preButton.clicked.connect(self.pre_img)
        self.nextButton.clicked.connect(self.next_img)


    def getImg_shiyan(self):
        img_path = QFileDialog.getExistingDirectory(None,"选取文件夹","")
        self.img_list = [img_path+'/'+i for i in os.listdir(img_path)]
        img = Image.open(self.img_list[0])
        self.shiyanlabel.setPixmap(ImageQt.toqpixmap(img).scaled(self.shiyanlabel.width(), self.shiyanlabel.height(), aspectMode=Qt.KeepAspectRatio))

    def pre_img(self):
        if self.img_list:
            if self.curr_img !=0:
               self.curr_img -= 1
            img = Image.open(self.img_list[self.curr_img])
            self.shiyanlabel.setPixmap(ImageQt.toqpixmap(img).scaled(self.shiyanlabel.width(), self.shiyanlabel.height(), aspectMode=Qt.KeepAspectRatio))

    def next_img(self):
        if self.img_list:
            if self.curr_img < len(self.img_list)-1:
               self.curr_img += 1
            else:
                QMessageBox.information(self, "提示", "没有下一张了")
            img = Image.open(self.img_list[self.curr_img])
            self.shiyanlabel.setPixmap(ImageQt.toqpixmap(img).scaled(self.shiyanlabel.width(), self.shiyanlabel.height(), aspectMode=Qt.KeepAspectRatio))



if __name__ == '__main__':
    app = QApplication([])
    window = myWindow()
    window.show()
    app.exec()

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值