You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用PySide6创建圆形进度条?已实现方案分享

使用PySide6实现圆形进度条

我之前一直琢磨怎么用PySide6做圆形进度条,现在找到了可行的实现方案,分享给大家,希望能帮到有需要的朋友。

效果展示

圆形进度条效果

实现代码

from PySide6.QtWidgets import QWidget, QApplication, QVBoxLayout
from PySide6.QtGui import QPainter, QPen, QColor
from PySide6.QtCore import Qt, QPropertyAnimation, QEasingCurve, Property
import sys

class CircularProgressBar(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self._progress = 0
        self.setFixedSize(150, 150)

    @Property(int)
    def progress(self):
        return self._progress

    @progress.setter
    def progress(self, value):
        self._progress = max(0, min(100, value))
        self.update()

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)

        # 绘制背景圆环
        pen = QPen(QColor(200, 200, 200), 10)
        painter.setPen(pen)
        painter.drawArc(10, 10, self.width()-20, self.height()-20, 0, 360*16)

        # 绘制进度圆环
        pen.setColor(QColor(50, 150, 250))
        pen.setCapStyle(Qt.RoundCap)
        painter.setPen(pen)
        arc_length = int((self._progress / 100) * 360 * 16)
        painter.drawArc(10, 10, self.width()-20, self.height()-20, 90*16, -arc_length)

        # 绘制进度文本
        painter.setPen(QColor(50, 50, 50))
        painter.drawText(self.rect(), Qt.AlignCenter, f"{self._progress}%")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = QWidget()
    layout = QVBoxLayout(window)

    progress_bar = CircularProgressBar()
    layout.addWidget(progress_bar, alignment=Qt.AlignCenter)

    # 添加动画演示进度变化
    animation = QPropertyAnimation(progress_bar, b"progress")
    animation.setDuration(2000)
    animation.setStartValue(0)
    animation.setEndValue(75)
    animation.setEasingCurve(QEasingCurve.InOutQuad)
    animation.start()

    window.show()
    sys.exit(app.exec())

简单说明

  • 自定义CircularProgressBar类继承自QWidget,通过重写paintEvent完成圆形进度条的绘制逻辑
  • 用QPropertyAnimation可以快速实现进度的平滑过渡动画
  • 尺寸、颜色、进度范围等参数都可以根据需求自行调整

内容的提问来源于stack exchange,提问作者ANIGLO Jonas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 20:05:34