2016-11-04 161 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