如何在pyQt5中获取ProgressBar的当前值?

时间:2019-06-07 08:25:09

标签: python progress-bar pyqt5

我正在学习Pyqt5。 问题是我想在pyqt5中获取progressbar的值

我尝试使用self.progressBar.getValue()self.progressBar.getInt()都不起作用

实际代码有点大,但是没关系。请帮助

我只需要从进度条获取当前值的语法,即:1到100之间

2 个答案:

答案 0 :(得分:2)

根据他们的documentation,获取值的方法仅为value(),因此在您的情况下,它将是self.progressBar.value()

答案 1 :(得分:0)

我同意@ dustin-we,这是一个最小的代码示例:

import sys
import time

from PyQt5.QtWidgets import (QApplication, QDialog,
                             QProgressBar, QPushButton)

TIME_LIMIT = 100


class Actions(QDialog):
    """
    Simple dialog that consists of a Progress Bar and a Button.
    Clicking on the button results in the start of a timer and
    updates the progress bar.
    """

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Progress Bar')
        self.progress = QProgressBar(self)
        self.progress.setGeometry(0, 0, 300, 25)
        self.progress.setMaximum(100)
        self.button = QPushButton('Start', self)
        self.button.move(0, 30)
        self.show()

        self.button.clicked.connect(self.onButtonClick)

    def onButtonClick(self):
        count = 0
        while count < TIME_LIMIT:
            count += 1
            time.sleep(0.01)
            self.progress.setValue(count)

            # !! Here is the command you need !!
            print(self.progress.value())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Actions()
    sys.exit(app.exec_())