簡體   English   中英

如何在pyQt5中獲取ProgressBar的當前值?

[英]How to get the current Value of ProgressBar in pyQt5?

我正在學習Pyqt5。 問題是我想在pyqt5中獲取progressbar的值

我嘗試使用self.progressBar.getValue()self.progressBar.getInt()self.progressBar.getInt()

實際的代碼有點大,但是沒關系。 請幫忙

我只需要從進度條獲取當前值的語法,即:1到100之間

根據他們的文檔 ,獲取值的方法只是value() ,所以在您的情況下,它將是self.progressBar.value()

我同意@ 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_())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM