import os
import sys
import time
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QHBoxLayout, QVBoxLayout
class ImageSlide(QWidget):
def __init__(self, image_dir):
super().__init__()
self.image_dir = image_dir
self.image_list = os.listdir(image_dir)
self.image_index = 0
self.paused = False
self.init_ui()
def init_ui(self):
self.setWindowTitle('Image Slide')
self.setGeometry(100, 100, 600, 400)
# 显示图片的标签
self.image_label = QLabel(self)
self.show_image()
# 开始、暂停、继续、备注 四个按钮
self.start_button = QPushButton('开始', self)
self.pause_button = QPushButton('暂停', self)
self.note_button = QPushButton('备注', self)
# 开始、暂停、继续 按钮和槽函数的绑定
self.start_button.clicked.connect(self.start_slide)
self.pause_button.clicked.connect(self.pause_or_resume_slide)
# 布局设置
hbox = QHBoxLayout()
hbox.addWidget(self.start_button)
hbox.addWidget(self.pause_button)
hbox.addWidget(self.note_button)
vbox = QVBoxLayout()
vbox.addWidget(self.image_label)
vbox.addLayout(hbox)
self.setLayout(vbox)
# 定时器设置,用于控制图片轮播
self.timer = QTimer(self)
self.timer.timeout.connect(self.show_next_image)
def show_image(self):
# 显示文件夹中的当前图片
image_path = os.path.join(self.image_dir, self.image_list[self.image_index])
pixmap = QPixmap(image_path)
self.image_label.setPixmap(pixmap.scaled(self.image_label.size(), Qt.KeepAspectRatio))
def show_next_image(self):
# 显示文件夹中的下一张图片
self.image_index = (self.image_index + 1) % len(self.image_list)
self.show_image()
def start_slide(self):
# 开始轮播
self.timer.start(1000) # 每10秒轮播一张图片
def pause_or_resume_slide(self):
# 暂停或继续轮播
self.paused = not self.paused
if self.paused:
self.timer.stop()
self.pause_button.setText('继续')
else:
self.timer.start()
self.pause_button.setText('暂停')
if __name__ == '__main__':
app = QApplication(sys.argv)
slide = ImageSlide('image_dir')
slide.show()
sys.exit(app.exec_())
pyqt5 实现图片轮播功能
最新推荐文章于 2024-05-01 09:59:16 发布