下面的代码对一堆 PyQt4 进度条进行了计数,达到 99%,我希望我的 GUI 不要 卡住,因为它们计数到了 99%。如果可能的话,我希望不使用自定义类或函数来做到这一点。 我知道使用类很好,但是对于这个小代码片段,我不想创建一个类。 根据我所读到的内容,可能有一个 update() 函数可以完成此任务...请告知我是否走在正确的轨道上
import sys
import time
from PyQt4 import QtGui
from PyQt4 import QtCore
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.resize(400, 200)
widget.setWindowTitle('simple')
widget.show()
shift = 0
cntControl = 5
barra = [0] * cntControl
for i in range(cntControl):
shift = shift + 10
barra[i] = QtGui.QProgressBar(widget)
barra[i].show()
barra[i].setMinimum(0)
barra[i].setMaximum(10000)
barra[i].setGeometry(QtCore.QRect(10, shift, 200, 10))
for a in range(10000):
for i in range(cntControl):
barra[i].setValue(a)
sys.exit(app.exec_())
最佳答案
尝试更改 for 循环:
while True:
for a in range(10000):
time.sleep(0.0001)
for i in range(cntControl):
barra[i].setValue(a)
如果对我有用。 while 循环继续不断地移动酒吧。如果您只想在到达终点后清理栏,您应该使用重置:
PySide.QtGui.QProgressBar.reset() Reset the progress bar. The progress bar “rewinds” and shows no progress
OP 评论后更新:如果您希望 gui 在进入长循环或其他操作时做出响应,您应该使用 python thread module或QThreads .
关于Python PyQt4 进度条卡住 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5743008/