pyqt实现图片轮播

在做界面时,有时需要做出动画的效果。简单的实现方式就是加载图片,然后轮播切换图片,从而达到动画效果。当然还可以使用属性动画,下次再上传。这里保存下代码,方便以后便于使用。


from PyQt5.Qt import *




class Window(QLabel):
    def __init__(self):
        super().__init__()
        self.n=1
        self.resize(500,500)
        self.setup_ui()
        self.timer=QTimer(self)
        self.timer.timeout.connect(self.timer_pic)
        self.timer.start(1000)

    def setup_ui(self):
        pic=QPixmap("../images/{}.jpg".format(str(self.n)))#拿到图片
        self.setPixmap(pic)#直接加载图片
        self.setScaledContents(True)#图片根据内容自适应
    def timer_pic(self):
        self.n+=1
        if self.n>5:
            self.n=1
        pic=QPixmap("../images/{}.jpg".format(str(self.n)))#拿到图片
        self.setPixmap(pic)
        self.setScaledContents(True)



if __name__ == '__main__':
    import sys
    app=QApplication(sys.argv)

    win=Window()
    win.show()

    sys.exit(app.exec_())



  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
您可以使用QTimer和QPixmap实现pyqt5标签图片轮播。下面是一个简单的示例代码: ```python from PyQt5.QtWidgets import QLabel, QWidget, QVBoxLayout, QApplication from PyQt5.QtGui import QPixmap from PyQt5.QtCore import QTimer import sys class ImageSlider(QWidget): def __init__(self, image_paths): super().__init__() self.image_paths = image_paths self.current_image_index = 0 self.image_label = QLabel() self.image_label.setFixedSize(400, 400) layout = QVBoxLayout() layout.addWidget(self.image_label) self.setLayout(layout) self.timer = QTimer() self.timer.timeout.connect(self.show_next_image) self.timer.start(2000) # 设置定时器间隔为2秒 def show_next_image(self): pixmap = QPixmap(self.image_paths[self.current_image_index]) self.image_label.setPixmap(pixmap) self.current_image_index = (self.current_image_index + 1) % len(self.image_paths) if __name__ == "__main__": app = QApplication(sys.argv) image_paths = ["image1.jpg", "image2.jpg", "image3.jpg"] slider = ImageSlider(image_paths) slider.show() sys.exit(app.exec_()) ``` 在上面的代码中,我们创建了一个名为ImageSlider的QWidget子类。它接受一个图像路径列表作为输入。在构造函数中,我们创建了一个QLabel用于显示图像,并使用QVBoxLayout将其添加到QWidget中。我们还创建了一个QTimer,每隔2秒调用show_next_image()方法以显示下一张图像。show_next_image()方法根据当前图像的索引从图像路径列表中加载图像,并将其设置为QLabel的Pixmap。最后,我们创建了一个QApplication实例并启动了事件循环。 您可以根据需要调整图像大小,间隔时间等参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值