查看我的代码并告诉我如何为 -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_())
您可以将进度控件的最大值设置为在输入窗口中输入的值,然后只需使用
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_())
导入 PyQt5.QtWidgets 作为 QtWidgets
if name == "main":
app = QtWidgets.QApplication([]) # this is for using PyQt5
# set a progressbar for showing the process
progressBar = QtWidgets.QProgressBar(None)
progressBar.setRange(0, len(datastring))
progressBar.resize(600,50)
progressBar.setWindowTitle("正在处理...请稍后")
progressBar.show()
for i in range(len(datastring)):
if(i%1000==0):
progressBar.setValue(i)**strong text**
if progressBar.isHidden():
progressBar.show()
QtWidgets.qApp.processEvents()
progressBar.close()