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