PyQt5 QProgressBar的使用

前言:

        在 PyQt5 中,QProgressBar 用于显示进度(如文件下载、任务执行进度等)。下面是一个完整的示例,展示如何在窗口中添加进度条并更新它:

基础示例:按钮控制进度条更新

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QProgressBar
from PyQt5.QtCore import QTimer

class ProgressBarDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("PyQt5 ProgressBar Example")
        self.setGeometry(100, 100, 300, 100)

        self.layout = QVBoxLayout()

        self.progress = QProgressBar(self)
        self.progress.setValue(0)
        self.progress.setMaximum(100)

        self.button = QPushButton("Start Progress", self)
        self.button.clicked.connect(self.start_progress)

        self.layout.addWidget(self.progress)
        self.layout.addWidget(self.button)
        self.setLayout(self.layout)

        self.timer = QTimer()
        self.timer.timeout.connect(self.update_progress)

        self.progress_value = 0

    def start_progress(self):
        self.progress_value = 0
        self.progress.setValue(self.progress_value)
        self.timer.start(50)  # update every 50 ms

    def update_progress(self):
        if self.progress_value >= 100:
            self.timer.stop()
        else:
            self.progress_value += 1
            self.progress.setValue(self.progress_value)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    demo = ProgressBarDemo()
    demo.show()
    sys.exit(app.exec_())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值