2016-11-04 154 views
0

我扩充了this example以允许我的用户知道程序在处理For循环中的RAM密集型和迭代过程时所处理的进程的哪一部分。我发现以下脚本只是要求打印计数值并等待几秒钟,但它不适用于更密集的功能。正在更新for循环中的PyQt4进度条 - Python 2.7

# from https://pythonspot.com/en/qt4-progressbar/ 

import sys 
from PyQt4.QtGui import * 
from PyQt4.QtCore import * 
from PyQt4.QtCore import pyqtSlot,SIGNAL,SLOT 
import time 

class QProgBar(QProgressBar): 

    value = 0 

    @pyqtSlot() 
    def increaseValue(progressBar): 
     progressBar.setValue(progressBar.value) 
     progressBar.value = progressBar.value+1 

# Create an PyQT4 application object. 
a = QApplication(sys.argv)  

# The QWidget widget is the base class of all user interface objects in PyQt4. 
w = QWidget() 

# Set window title 
w.setWindowTitle("PyQT4 Progressbar @ pythonspot.com ") 

# Create progressBar. 
bar = QProgBar(w) 
bar.resize(320,50)  
bar.setValue(0) 


bar.setAlignment(Qt.AlignCenter) 
bar.move(0,50) 

label = QLabel("test",w) 
label.setStyleSheet("QLabel { font-size: 20px }") 
label.setAlignment(Qt.AlignCenter) 
label.move(0,10) 

# create timer for progressBar 
'''timer = QTimer() 
bar.connect(timer,SIGNAL("timeout()"),bar,SLOT("increaseValue()")) 
timer.start(400)''' 

# Show window 
w.show() 

for i in range(0,100): 
    print(i) 
    ### Do action 
    bar.setValue(i) 
    time.sleep(0.5) 

sys.exit(a.exec_()) 

有什么我可以做的,使这项工作更激烈的行动?有没有人知道更好的进度条包,我可以轻松地插入for循环?

我是一般的GUI开发新手,我真的只需要简单的用户界面。

预先感谢任何帮助,您可以提供:),直到你把控制权返回给事件循环

回答

1

的Qt将不会更新您的UI,也就是说,直到你的循环结束。您可以通过调用processEvents来使事件循环更新UI,如下所示:

for i in range(0,100): 
    print(i) 
    ### Do action 
    bar.setValue(i) 
    a.processEvents() 
    time.sleep(0.5) 
+0

太棒了!谢谢!!!! – user2312900

+0

不客气。如果它解决了您的问题,请将我的答案标记为已接受。 – titusjan