简体   繁体   English

PyQt5:如何在PyQt5中使用进度栏?

[英]PyQt5 : How to use progress bar in PyQt5?

See my code and tell me how do I set progress bar to the -dollar()- function and progress bar start with doing the function and finished with it. 查看我的代码,并告诉我如何将进度条设置为-dollar()-函数,并且进度条首先执行该功能,然后完成该功能。 see my code at the continue: 继续查看我的代码:

from PyQt5.QtWidgets import (QWidget,QApplication,QTextEdit,
    QInputDialog,QPushButton,QVBoxLayout,QProgressBar)
import sys

class Tbx(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.vbox = QVBoxLayout()
        self.btn = QPushButton('ClickMe',self)
        self.btn.clicked.connect(self.dollar)
        self.te = QTextEdit(self)
        self.prgb = QProgressBar(self)#How to set this to doing something?
        self.vbox.addWidget(self.te)
        self.vbox.addWidget(self.btn)
        self.vbox.addWidget(self.prgb)
        self.setLayout(self.vbox)
        self.setGeometry(300,300,400,250)
        self.setWindowTitle('Application')
        self.show()
    def dollar(self):#Like doing this set progress bar to this
        text_1_int , ok = QInputDialog.getInt(self,'HowMany?','Enter How Many dollar do you want ?')
        if not ok:
            return
        current_lines = self.te.toPlainText().split('\n')
        new_lines = list()
        for dollar_counter in range(1,text_1_int+1):
            word = '$'*dollar_counter
            new_lines += [word + text for text in current_lines]
        self.te.setPlainText('\n'.join(new_lines))
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Tbx()
    sys.exit(app.exec_())

You can set the maximum value of the progress control to the value entered in your input window and then simply use setValue to increase the progress bar, however, this will block the UI for very large calculations, so you might want to also move your method to a new thread and report progress to the UI using a signal, here is the full example: 您可以将进度控件的最大值设置为在输入窗口中输入的值,然后只需使用setValue来增加进度栏,但是,这将阻止UI进行非常大的计算,因此您可能还希望移动方法到新线程并使用信号向UI报告进度,以下是完整示例:

from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import (QWidget, QApplication, QTextEdit, QInputDialog, QPushButton, QVBoxLayout, QProgressBar)
import sys


class DollarCalculation(QThread):
    reportProgress = pyqtSignal(int, list)
    calculationFinished = pyqtSignal()

    def __init__(self, numDollars, currentLines):
        super().__init__()

        self.numDollars = numDollars
        self.currentLines = currentLines

    def run(self) -> None:
        for dollar_counter in range(1, self.numDollars + 1):
            word = '$' * dollar_counter
            self.reportProgress.emit(dollar_counter + 1, [word + text for text in self.currentLines])

        self.calculationFinished.emit()


class Tbx(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

        self.dollarCalculation = None

    def initUI(self):
        self.vbox = QVBoxLayout()
        self.btn = QPushButton('ClickMe', self)
        self.btn.clicked.connect(self.dollar)
        self.te = QTextEdit(self)
        self.prgb = QProgressBar(self)
        self.vbox.addWidget(self.te)
        self.vbox.addWidget(self.btn)
        self.vbox.addWidget(self.prgb)
        self.setLayout(self.vbox)
        self.setGeometry(300, 300, 400, 250)
        self.setWindowTitle('Application')


    def dollar(self):
        text_1_int, ok = QInputDialog.getInt(self, 'HowMany?', 'Enter How Many dollar do you want ?')
        if not ok:
            return

        self.btn.setEnabled(False)

        self.prgb.setMaximum(text_1_int + 1)
        self.dollarCalculation = DollarCalculation(text_1_int, self.te.toPlainText().split('\n'))
        self.dollarCalculation.reportProgress.connect(self.progress)
        self.dollarCalculation.calculationFinished.connect(self.calculationFinished)
        self.dollarCalculation.start()

    def progress(self, value, newLines):
        self.te.append('\n'.join(newLines))
        self.prgb.setValue(value)


    def calculationFinished(self):
        self.btn.setEnabled(True)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Tbx()
    ex.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM