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_())




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM