forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RoundProgressBar.py
112 lines (92 loc) · 3.09 KB
/
RoundProgressBar.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2018年9月4日
@author: Irony
@site: https://pyqt5.com , https://github.com/892768447
@email: 892768447@qq.com
@file: 界面美化.圆形进度条.CircleProgressBar
@description:
"""
from PyQt5.QtCore import QSize, pyqtProperty, QTimer, Qt
from PyQt5.QtGui import QColor, QPainter
from PyQt5.QtWidgets import QWidget, QHBoxLayout
__Author__ = """By: Irony
QQ: 892768447
Email: 892768447@qq.com"""
__Copyright__ = "Copyright (c) 2018 Irony"
__Version__ = "Version 1.0"
class CircleProgressBar(QWidget):
Color = QColor(24, 189, 155) # 圆圈颜色
Clockwise = True # 顺时针还是逆时针
Delta = 36
def __init__(self, *args, color=None, clockwise=True, **kwargs):
super(CircleProgressBar, self).__init__(*args, **kwargs)
self.angle = 0
self.Clockwise = clockwise
if color:
self.Color = color
self._timer = QTimer(self, timeout=self.update)
self._timer.start(100)
def paintEvent(self, event):
super(CircleProgressBar, self).paintEvent(event)
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.translate(self.width() / 2, self.height() / 2)
side = min(self.width(), self.height())
painter.scale(side / 100.0, side / 100.0)
painter.rotate(self.angle)
painter.save()
painter.setPen(Qt.NoPen)
color = self.Color.toRgb()
for i in range(11):
color.setAlphaF(1.0 * i / 10)
painter.setBrush(color)
painter.drawEllipse(30, -10, 20, 20)
painter.rotate(36)
painter.restore()
self.angle += self.Delta if self.Clockwise else -self.Delta
self.angle %= 360
@pyqtProperty(QColor)
def color(self) -> QColor:
return self.Color
@color.setter
def color(self, color: QColor):
if self.Color != color:
self.Color = color
self.update()
@pyqtProperty(bool)
def clockwise(self) -> bool:
return self.Clockwise
@clockwise.setter
def clockwise(self, clockwise: bool):
if self.Clockwise != clockwise:
self.Clockwise = clockwise
self.update()
@pyqtProperty(int)
def delta(self) -> int:
return self.Delta
@delta.setter
def delta(self, delta: int):
if self.delta != delta:
self.delta = delta
self.update()
def sizeHint(self) -> QSize:
return QSize(100, 100)
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
layout = QHBoxLayout(self)
layout.addWidget(CircleProgressBar(self))
layout.addWidget(CircleProgressBar(
self, color=QColor(255, 0, 0), clockwise=False))
layout.addWidget(CircleProgressBar(self, styleSheet="""
qproperty-color: rgb(0, 255, 0);
"""))
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())