Contents

原创作品,允许转载。转载时请务必以超链接形式标明文章原始出处、作者信息和本声明,否则将追究法律责任。

把下面代码加到你自定义的Widget里面即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class MyWidget(QtGui.QWidget):
def __init__(self)
self._visible = False
self._fadeInAniFinished = True
self._fadeOutAniFinished = True
self._fadeInAni = None
self._fadeOutAni = None
self._initAni()
def _initAni(self):
"""
初始化动画效果
:return:
"""
self._fadeInAni = QtCore.QPropertyAnimation(self, "windowOpacity")
self._fadeInAni.setDuration(100)
self._fadeInAni.setStartValue(0)
self._fadeInAni.setEndValue(1)
self._fadeInAni.finished.connect(self._setFadeInAniFinished)
self._fadeOutAni = QtCore.QPropertyAnimation(self, "windowOpacity")
self._fadeOutAni.setDuration(150)
self._fadeOutAni.setStartValue(1)
self._fadeOutAni.setEndValue(0)
self._fadeOutAni.finished.connect(self._setFadeOutAniFinished)
def setVisible(self, bool_visible):
"""
设置是否可见
:param bool_visible:
:return:
"""
self._visible = bool_visible
if bool_visible:
if not self._fadeOutAniFinished:
self._fadeOutAni.stop()
self._setFadeOutAniFinished()
if not self._fadeInAniFinished:
return
self._fadeInAniFinished = False
super(TipsWindow, self).setVisible(True)
self._fadeInAni.start()
else:
if not self._fadeInAniFinished:
self._fadeInAni.stop()
self._setFadeInAniFinished()
if not self._fadeOutAniFinished:
return
self._fadeOutAniFinished = False
self._fadeOutAni.start()
def isVisible(self):
"""
是否可见
:return:
"""
return self._visible
def _setFadeInAniFinished(self):
"""
淡入动画结束
:return:
"""
self._fadeInAniFinished = True
def _setFadeOutAniFinished(self):
"""
淡出动画结束
:return:
"""
super(TipsWindow, self).setVisible(False)
self._fadeOutAniFinished = True
Contents