在PyQt中实现一次性选择多张图片,并通过按钮切换到下一张图片的功能

 效果:

代码 

from PyQt5 import QtWidgets, QtGui

class ImageViewer(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.selected_imgsPath = []
        self.current_image_index = 0
        
        self.label_3 = QtWidgets.QLabel(self)
        self.label_3.setFixedSize(500, 500)  # 设置标签大小为500x500
        self.lineEdit_3 = QtWidgets.QLineEdit(self)
        self.next_image_button = QtWidgets.QPushButton("下一张图片", self)
        self.next_image_button.clicked.connect(self.showNextImage)
        self.prev_image_button = QtWidgets.QPushButton("上一张图片", self)
        self.prev_image_button.clicked.connect(self.showPrevImage)
        
        self.layout = QtWidgets.QVBoxLayout(self)
        self.layout.addWidget(self.label_3)
        self.layout.addWidget(self.lineEdit_3)
        self.layout.addWidget(self.next_image_button)
        self.layout.addWidget(self.prev_image_button)
        
        self.openImage()  # 调用 openImage 函数显示第一张图片
        
    def openImage(self):
        self.selected_imgsPath, _ = QtWidgets.QFileDialog.getOpenFileNames(
            self, "打开图片", "./pending_images", "*.jpg;;*.png;;All Files(*)")
        if len(self.selected_imgsPath) == 0:
            self.empty_information()
            print('empty!')
            return

        img = QtGui.QPixmap(self.selected_imgsPath[0]).scaled(500, 500)  # 调整图片大小为500x500
        self.label_3.setPixmap(img)
        self.lineEdit_3.setText(self.selected_imgsPath[0])
        
        self.current_image_index = 0
        
    def showNextImage(self):
        if len(self.selected_imgsPath) == 0:
            return

        self.current_image_index += 1
        if self.current_image_index >= len(self.selected_imgsPath):
            self.current_image_index = 0

        img_path = self.selected_imgsPath[self.current_image_index]
        img = QtGui.QPixmap(img_path).scaled(500, 500)  # 调整图片大小为500x500
        self.label_3.setPixmap(img)
        self.lineEdit_3.setText(img_path)
        
    def showPrevImage(self):
        if len(self.selected_imgsPath) == 0:
            return

        self.current_image_index -= 1
        if self.current_image_index < 0:
            self.current_image_index = len(self.selected_imgsPath) - 1

        img_path = self.selected_imgsPath[self.current_image_index]
        img = QtGui.QPixmap(img_path).scaled(500, 500)  # 调整图片大小为500x500
        self.label_3.setPixmap(img)
        self.lineEdit_3.setText(img_path)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    viewer = ImageViewer()
    viewer.show()
    sys.exit(app.exec_())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值