PyQt5设置图片格式及动画

1、缩放图片
'''
使用QImage.Scale(width,height)方法可以来设置图片
'''
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys

class scaleimage(QWidget):
def __init__(self):
super(scaleimage,self).__init__()
self.setWindowTitle("缩放图片大小")

filename="./image/0.jpg"
img=QImage(filename)
label=QLabel(self)
label.setFixedWidth(200)
label.setFixedHeight(200)

#第一种方式-手动设置图片的大小尺寸为label的宽度和高度
#忽略比例,平滑化处理
result=img.scaled(label.width(),label.height(),Qt.IgnoreAspectRatio,Qt.SmoothTransformation)
label.setPixmap(QPixmap.fromImage(result))

#第二种方式-设置自比例缩放大小
label2 = QLabel(self)
label2.setFixedWidth(200)
label2.setFixedHeight(200)
label2.setScaledContents(True) #设置自动比例缩放的方式
label2.setPixmap(QPixmap("./image/0.jpg")) # 设置图片显示形式

v=QVBoxLayout()
v.addWidget(label)
v.addWidget(label2)
self.setLayout(v)

if __name__=="__main__":
app=QApplication(sys.argv)
p=scaleimage()
p.show()
sys.exit(app.exec_())

2、装载GIF动画
'''
使用QMovie
'''
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys

class loadgif(QWidget):
def __init__(self):
super(loadgif,self).__init__()
self.setWindowTitle("装载GIF动画")
self.setFixedSize(512,288)
#self.setWindowFlags(Qt.Dialog|Qt.CustomizeWindowHint) #隐藏标题栏,窗口样式

self.movie=QMovie("./image/load.gif") #加载本地动画文件GIF文件
self.label=QLabel(self)
self.label.setMovie(self.movie)
self.movie.start() #开启动画

if __name__=="__main__":
app=QApplication(sys.argv)
p=loadgif()
p.show()
sys.exit(app.exec_())

3、动画效果改变窗口的尺寸
'''
QPropertyAnimation对象
用来控制动态变化的对象
可以动态改变控件的尺寸
'''
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys

class animwindow(QWidget):
def __init__(self):
super(animwindow,self).__init__()
self.setWindowTitle("动态效果改变窗口的尺寸")
self.OrigHeight=50
self.ChangeHeight=150
self.setGeometry(QRect(500,400,150,self.OrigHeight))
self.b1=QPushButton("展开",self)
self.b1.setGeometry(10,10,60,35)
self.b1.clicked.connect(self.change)

def change(self):
currentheight=self.height()
if self.OrigHeight==currentheight:
startheight=self.OrigHeight
endheight=self.ChangeHeight
self.b1.setText("收缩")
else:
startheight = self.ChangeHeight
endheight = self.OrigHeight
self.b1.setText("展开")

#利用QPropertyAnimation(self,b'geometry')来进行设置动态改变窗口的尺寸
self.animation=QPropertyAnimation(self,b'geometry')
self.animation.setDuration(500) #设置动态变化的时间间隔为0.5秒
self.animation.setStartValue(QRect(500,400,150,startheight))
self.animation.setEndValue(QRect(500,400,300,endheight))
self.animation.start() #开始动态变化

if __name__=="__main__":
app=QApplication(sys.argv)
p=animwindow()
p.show()
sys.exit(app.exec_())



4、实现不同移动速度移动窗口
'''
'''
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys

app=QApplication(sys.argv)
window1=QMainWindow()
window1.show()
window2=QMainWindow()
window2.show()

animation1=QPropertyAnimation(window1,b'geometry')
animation2=QPropertyAnimation(window2,b'geometry')

#两个窗口动态并行的方式
#group=QParallelAnimationGroup()
#两个窗口动态串行方式
group=QSequentialAnimationGroup()
group.addAnimation(animation1)
group.addAnimation(animation2)

animation1.setDuration(3000) #设置动画的时间间隔为3秒
animation1.setStartValue(QRect(0,0,100,30))
animation1.setEndValue(QRect(250,250,100,30))
animation1.setEasingCurve(QEasingCurve.OutBounce) #设置动画变化的实际动态曲线效果形式

animation2.setDuration(4000)
animation2.setStartValue(QRect(250,250,100,30))
animation2.setEndValue(QRect(850,250,100,30))
animation2.setEasingCurve(QEasingCurve.CosineCurve) #设置动画的形式曲线

group.start()
sys.exit(app.exec_())



posted @ 2020-02-18 11:20  The-Chosen-One  阅读(2312)  评论(0编辑  收藏  举报