以下是实现的完整的代码:
import sys
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QPainter, QColor, QFont, QPen
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout
class CircularProgress(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self._percent = 0
self._timer = QTimer(self)
self._timer.timeout.connect(self.update_progress)
self._timer.start(100) # 设置更新间隔为100ms
def set_percent(self, percent):
self._percent = percent
self.update()
def update_progress(self):
self._percent += 1
if self._percent > 100:
self._percent = 0
self.update()
def paintEvent(self, event):
width, height = self.width(), self.height()
side = min(width, height)