我正在尝试制作一个在我正在运行的漫长过程中运行的脉动进度条,当它完成时它将完全变为绿色。我在这里已经阅读了一些以前的帖子,但我无法弄清楚我需要的线程。
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_())
现在酒吧只是脉动。它永远不会进入测试def。我知道这是因为我没有正确地调用它,但我不确定如何使条形脉冲和def测试同时运行。
答案 0 :(得分:2)
好吧,要让进度条旋转时test()
方法有效,你可以这样做:
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()
并从test()
中移除Ui_form
方法。整个想法是你需要在另一个线程中执行长期运行的任务而不是处理主窗口的主线程。
答案 1 :(得分:1)
test()
方法与进度条无关,因为您目前正在设计它。 TaskThread.run()
方法必须使用其长时间运行的当前进度发出一些信号。此信号必须连接到插槽progressBar.setValue()
。因此,当TaskThread
中的操作稍微进展时,会向进度条发送一个信号,以实际更新该进度。
这是一个大纲,为了简洁我省略了很多代码
class Ui_Form():
__init__(self, Form):
# Omitted
# Connect progress of task thread with progress bar
myLongTask.valueChanged.connect(self.progressBar.setValue)
def onStart(self):
# QProgressBar uses integers to represent range and value
self.progressBar.setRange(0, 100)
self.myLongTask.start()
class TaskThread():
valueChanged = QtCore.pyqtSignal(int)
def run():
for i in range(100):
time.sleep(0.01) # Do "work"
self.valueChanged.emit(i) # Notify progress bar to update via signal
self.taskFinished.emit()