Skip to content

Commit 08fb61f

Browse files
committed
窗口抖动
1 parent bcd2223 commit 08fb61f

File tree

5 files changed

+87
-3
lines changed

5 files changed

+87
-3
lines changed

.settings/org.eclipse.core.resources.prefs

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ encoding//QProgressBar/PercentProgressBar.py=utf-8
3636
encoding//QProgressBar/SimpleStyle.py=utf-8
3737
encoding//QProgressBar/WaterProgressBar.py=utf-8
3838
encoding//QPropertyAnimation/Lib/SlidingStackedWidget.py=utf-8
39+
encoding//QPropertyAnimation/MenuAnimation.py=utf-8
40+
encoding//QPropertyAnimation/ShakeWindow.py=utf-8
3941
encoding//QProxyStyle/Lib/TabBarStyle.py=utf-8
4042
encoding//QProxyStyle/TabTextDirection.py=utf-8
4143
encoding//QPushButton/BottomLineProgress.py=utf-8

QPropertyAnimation/README.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
- [右键菜单动画](#2、右键菜单动画)
66
- [点阵特效](#3、点阵特效)
77
- [页面切换/图片轮播动画](#4、页面切换/图片轮播动画)
8+
- [窗口抖动](#5、窗口抖动)
89

9-
# 1、窗口淡入淡出
10+
## 1、窗口淡入淡出
1011
[运行 FadeInOut.py](FadeInOut.py)
1112

1213
1. 使用`QPropertyAnimation`对窗口的`windowOpacity`透明度属性进行修改
@@ -19,7 +20,7 @@
1920

2021
![FadeInOut](ScreenShot/FadeInOut.gif)
2122

22-
# 2、右键菜单动画
23+
## 2、右键菜单动画
2324
[运行 MenuAnimation.py](MenuAnimation.py)
2425

2526
1. 使用`QPropertyAnimation`对菜单控件的`geometry`属性进行修改
@@ -116,4 +117,11 @@ def findClose(points):
116117
1. `setCurrentIndex` 切换到指定页
117118
1. `autoStart(msec)` 轮播模式, 默认是3000毫秒
118119

119-
![PageSwitching](ScreenShot/PageSwitching.gif)
120+
![PageSwitching](ScreenShot/PageSwitching.gif)
121+
122+
## 5、窗口抖动
123+
[运行 ShakeWindow.py](ShakeWindow.py)
124+
125+
通过`QPropertyAnimation`对控件的pos属性进行死去活来的修改
126+
127+
![ShakeWindow](ScreenShot/ShakeWindow.gif)
104 KB
Loading

QPropertyAnimation/ShakeWindow.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Created on 2019年5月8日
6+
@author: Irony
7+
@site: https://pyqt5.com https://github.com/892768447
8+
@email: 892768447@qq.com
9+
@file: ShakeWindow
10+
@description: 抖动动画
11+
"""
12+
from PyQt5.QtCore import QPropertyAnimation, QPoint
13+
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton
14+
15+
16+
__Author__ = 'Irony'
17+
__Copyright__ = 'Copyright (c) 2019 Irony'
18+
__Version__ = 1.0
19+
20+
21+
class Window(QWidget):
22+
23+
def __init__(self, *args, **kwargs):
24+
super(Window, self).__init__(*args, **kwargs)
25+
self.resize(400, 400)
26+
layout = QVBoxLayout(self)
27+
layout.addWidget(QPushButton('抖动', self, clicked=self.doShake))
28+
29+
def doShake(self):
30+
self.doShakeWindow(self)
31+
32+
# 下面这个方法可以做成这样的封装给任何控件
33+
def doShakeWindow(self, target):
34+
"""窗口抖动动画
35+
:param target: 目标控件
36+
"""
37+
if hasattr(target, '_shake_animation'):
38+
# 如果已经有该对象则跳过
39+
return
40+
41+
animation = QPropertyAnimation(target, b'pos', target)
42+
target._shake_animation = animation
43+
animation.finished.connect(lambda: delattr(target, '_shake_animation'))
44+
45+
pos = target.pos()
46+
x, y = pos.x(), pos.y()
47+
48+
animation.setDuration(200)
49+
animation.setLoopCount(2)
50+
animation.setKeyValueAt(0, QPoint(x, y))
51+
animation.setKeyValueAt(0.09, QPoint(x + 2, y - 2))
52+
animation.setKeyValueAt(0.18, QPoint(x + 4, y - 4))
53+
animation.setKeyValueAt(0.27, QPoint(x + 2, y - 6))
54+
animation.setKeyValueAt(0.36, QPoint(x + 0, y - 8))
55+
animation.setKeyValueAt(0.45, QPoint(x - 2, y - 10))
56+
animation.setKeyValueAt(0.54, QPoint(x - 4, y - 8))
57+
animation.setKeyValueAt(0.63, QPoint(x - 6, y - 6))
58+
animation.setKeyValueAt(0.72, QPoint(x - 8, y - 4))
59+
animation.setKeyValueAt(0.81, QPoint(x - 6, y - 2))
60+
animation.setKeyValueAt(0.90, QPoint(x - 4, y - 0))
61+
animation.setKeyValueAt(0.99, QPoint(x - 2, y + 2))
62+
animation.setEndValue(QPoint(x, y))
63+
64+
animation.start(animation.DeleteWhenStopped)
65+
66+
67+
if __name__ == '__main__':
68+
import sys
69+
from PyQt5.QtWidgets import QApplication
70+
app = QApplication(sys.argv)
71+
w = Window()
72+
w.show()
73+
sys.exit(app.exec_())

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站
145145
- [右键菜单动画](QPropertyAnimation/MenuAnimation.py)
146146
- [点阵特效](QPropertyAnimation/RlatticeEffect.py)
147147
- [页面切换/图片轮播动画](QPropertyAnimation/PageSwitching.py)
148+
- [窗口抖动](QPropertyAnimation/ShakeWindow.py)
148149
- [折叠动画](Test/partner_625781186/2.折叠控件)
149150

150151
- Others

0 commit comments

Comments
 (0)