长过程中的PYQT和进度条 [英] PYQT and progress Bar during Long Process
问题描述
我正在尝试制作一个脉动进度条,该进度条在我正在运行的漫长过程中运行,完成后它将完全变为绿色.我在这里阅读了一些以前的帖子,但我无法弄清楚我需要的线程.
I am trying to make a pulsating progress bar that runs during a long process I am running and when it is done it will completely turn green. I have read some previous post on here but I can't figure out the threading I need for this.
import time
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(770, 726)
self.progressBar = QtGui.QProgressBar(Form)
self.progressBar.setGeometry(QtCore.QRect(150, 590, 118, 23))
self.progressBar.setProperty("value", 24)
self.progressBar.setTextVisible(False)
self.progressBar.setObjectName(_fromUtf8("progressBar"))
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(25, 50, 170, 40))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton.clicked.connect(self.onStart)
self.myLongTask = TaskThread()
self.myLongTask.taskFinished.connect(self.onFinished)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def onStart(self):
self.progressBar.setRange(0,0)
self.myLongTask.start()
def onFinished(self):
# Stop the pulsation
self.progressBar.setRange(0,1)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.pushButton.setText(_translate("Form", "Run", None))
def test(self):
for i in xrange(500000):
print i
class TaskThread(QtCore.QThread):
taskFinished = QtCore.pyqtSignal()
def run(self):
time.sleep(3)
self.taskFinished.emit()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
现在酒吧只是脉动.它永远不会进入测试定义.我知道这是因为我没有正确调用它,但我不确定如何同时运行 bar pulsate 和 def 测试.
Right now the bar just pulsates. It never gets to the test def. i know this is because I am not calling it properly, but I am not sure how to have the bar pulsate and the def test run at the same time.
推荐答案
好吧,要让 test()
方法在进度条旋转时工作,您可以这样做:
well, to have the test()
method work while the progress bar is spinning you can do:
class TaskThread(QtCore.QThread):
taskFinished = QtCore.pyqtSignal()
def run(self):
# instead of sleeping do the long running loop
for i in xrange(500000):
print i
self.taskFinished.emit()
并从 Ui_form
中删除 test()
方法.整个想法是,您需要在另一个线程中执行长时间运行的任务,而不是处理主窗口的主线程.
and remove the test()
method from Ui_form
. The whole idea being that you need to have the long running task within another thread than the main thread handling the main window.
这篇关于长过程中的PYQT和进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!