当前位置:   article > 正文

PyQt5入门(二十七)装载gif动画文件 & 缩放图片 & 动画效果操作窗口_pyqt5界面放一个动态图片

pyqt5界面放一个动态图片

目录

一.装载gif动画文件

 二.缩放图片

三.用动画效果改变窗口的尺寸

四.用动画效果——不同速度移动窗口


一.装载gif动画文件

代码:

  1. import sys
  2. from PyQt5.QtWidgets import *
  3. from PyQt5.QtGui import *
  4. from PyQt5.QtCore import *
  5. class loadingGif(QWidget):
  6. def __init__(self):
  7. super(loadingGif, self).__init__()
  8. self.label=QLabel("",self)
  9. #fixed adj. 确定的;固执的
  10. self.setFixedSize(128,128)
  11. self.resize(400,300)#设置了固定尺寸之后,尺寸便不可修改!
  12. self.setWindowFlags(Qt.Dialog | Qt.CustomizeWindowHint)
  13. self.movie=QMovie('../picture/images/loading.gif')
  14. self.label.setMovie(self.movie)
  15. self.movie.start()
  16. if __name__ == "__main__":
  17. app = QApplication(sys.argv)
  18. form = loadingGif()
  19. form.show()
  20. sys.exit(app.exec_())

运行结果:

 二.缩放图片

QImage.scaled

代码:

  1. from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout
  2. from PyQt5.QtGui import QImage, QPixmap
  3. from PyQt5.QtCore import Qt
  4. import sys
  5. class ScaleImage(QWidget):
  6. def __init__(self):
  7. super().__init__()
  8. self.setWindowTitle("图片大小缩放例子")
  9. filename = '../picture/images/Cloudy_72px.png'
  10. img = QImage(filename)
  11. label1 = QLabel(self)
  12. label1.setFixedWidth(200)
  13. label1.setFixedHeight(200)
  14. #参数三四:忽略比例,平滑显示
  15. result = img.scaled(label1.width(),label1.height(),Qt.IgnoreAspectRatio,Qt.SmoothTransformation)
  16. label1.setPixmap(QPixmap.fromImage(result))
  17. vbox = QVBoxLayout()
  18. vbox.addWidget(label1)
  19. self.setLayout(vbox)
  20. if __name__ == "__main__":
  21. app = QApplication(sys.argv)
  22. win = ScaleImage()
  23. win.show()
  24. sys.exit(app.exec_())

运行结果:

三.用动画效果改变窗口的尺寸

QPropertyAnimation可以控制任何可视控件的尺寸的动态的变化,只要我们把控件的对象通过构造方法传入即可。

代码:

  1. from PyQt5.QtCore import *
  2. from PyQt5.QtWidgets import *
  3. import sys
  4. class AnimWindow(QWidget):
  5. def __init__(self):
  6. super(AnimWindow, self).__init__()
  7. self.OrigHeight = 50
  8. self.ChangeHeight = 150
  9. self.setGeometry(QRect(500, 400, 150, self.OrigHeight))
  10. self.btn = QPushButton('展开', self) #没用布局,直接将按钮放入
  11. self.btn.setGeometry(10, 10, 60, 35)
  12. self.btn.clicked.connect(self.change)
  13. def change(self):
  14. currentHeight = self.height()
  15. if self.OrigHeight == currentHeight:
  16. startHeight = self.OrigHeight
  17. endHeight = self.ChangeHeight
  18. self.btn.setText('收缩')
  19. else:
  20. startHeight = self.ChangeHeight
  21. endHeight= self.OrigHeight
  22. self.btn.setText('展开')
  23. self.animation = QPropertyAnimation(self,b'geometry')#将当前窗口传入
  24. self.animation.setDuration(500) #间隔时间:500毫秒
  25. #初始尺寸
  26. self.animation.setStartValue(QRect(500,400,150,startHeight))
  27. #变化后的尺寸
  28. self.animation.setEndValue(QRect(500,400,150,endHeight))
  29. self.animation.start()
  30. return
  31. if __name__ == '__main__':
  32. app = QApplication(sys.argv)
  33. window = AnimWindow()
  34. window.show()
  35. sys.exit(app.exec_())

运行结果:

            

四.用动画效果——不同速度移动窗口

代码:

  1. from PyQt5.QtGui import *
  2. from PyQt5.QtCore import *
  3. from PyQt5.QtWidgets import *
  4. import sys
  5. app = QApplication(sys.argv)
  6. window1 = QMainWindow()
  7. window1.show()
  8. window2 = QMainWindow()
  9. window2.show()
  10. animation1 = QPropertyAnimation(window1, b'geometry')
  11. animation2 = QPropertyAnimation(window2, b'geometry')
  12. group = QParallelAnimationGroup() # 并行 动画组,同时运行多个动画
  13. #group = QSequentialAnimationGroup() # 串行
  14. group.addAnimation(animation1)
  15. group.addAnimation(animation2)
  16. animation1.setDuration(3000)
  17. animation1.setStartValue(QRect(0,0,100,30))
  18. animation1.setEndValue(QRect(250,250,100,30))
  19. animation1.setEasingCurve(QEasingCurve.OutBounce) #动画特效
  20. animation2.setDuration(4000)
  21. animation2.setStartValue(QRect(250,150,100,30))
  22. animation2.setEndValue(QRect(850,250,100,30))
  23. animation2.setEasingCurve(QEasingCurve.CosineCurve)
  24. group.start()
  25. sys.exit(app.exec())

运行结果:

一组窗口的动画,自己试试吧!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/729869
推荐阅读
相关标签
  

闽ICP备14008679号