python-3.x PyQt5:如何在PyQt5中使用进度条?

guykilcj  于 12个月前  发布在  Python
关注(0)|答案(2)|浏览(158)

查看我的代码,告诉我如何将进度条设置为-dollar()-函数,并从执行函数开始进度条并完成它。

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_())

字符串

sgtfey8w

sgtfey8w1#

您可以将进度控制的最大值设置为在输入窗口中输入的值,然后简单地使用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_())

字符串

wa7juj8i

wa7juj8i2#

您可以在不使用任何已定义类的情况下使用进度条
将PyQt5.QtWidgets导入为QtWidgets
ifname==“main":

app = QtWidgets.QApplication([]) # this is for using PyQt5

# set a progressbar for showing the process
progressBar = QtWidgets.QProgressBar(None)
progressBar.setRange(0, 21000000)
progressBar.resize(600,50)
progressBar.setWindowTitle("正在处理...请稍后")
progressBar.show()

for i in range(21000000):
    if(i%1000==0):
        progressBar.setValue(i)
        if progressBar.isHidden():
           progressBar.show()
           QtWidgets.qApp.processEvents()
progressBar.close()

字符串

相关问题