Python:如何将多个进度条链接到以Threading开头的每个子进程

时间:2014-03-14 19:31:36

标签: python multithreading qt

将创建与 myList 中的实体一样多的进度条。 单击“确定”按钮将启动与 myList 中的实体一样多的子流程。

问题: 1.似乎有更多的子流程开始比 myList 中的实体。 2.无法使用一行更新ProgressBar(这就是注释的原因):

pb.update_bar( self.pBars[each]['value'] ) 

import sys, os
from PyQt4 import QtCore, QtGui
import threading
import time


class PbWidget(QtGui.QProgressBar):
    def __init__(self, parent=None, total=20):
        super(PbWidget, self).__init__()
        self.setMinimum(1)
        self.setMaximum(total)        
        self._active = False  

    def update_bar(self, to_add_number):
        while True:
            time.sleep(0.01)
            value = self.value() + to_add_number            
            self.setValue(value)
            QtGui.qApp.processEvents()
            if (not self._active or value >= self.maximum()):                
                break
        self._active = False

    def closeEvent(self, event):
        self._active = False

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.myList = ['One','Two','Three','Four','Five','Six','Seven']

        self.main_layout = QtGui.QVBoxLayout()
        self.pBars={}

        for each in self.myList:
            pb=PbWidget(total=101)
            self.main_layout.addWidget(pb)
            self.pBars[each]={'pb':pb, 'name':each, 'value': 0} 

        ok_button = QtGui.QPushButton("Distribute")
        ok_button.clicked.connect(self.OK)      
        self.main_layout.addWidget(ok_button)       

        central_widget = QtGui.QWidget()
        central_widget.setLayout(self.main_layout)
        self.setCentralWidget(central_widget)


    def internalFunc(self, myEvent, each):

        pb = self.pBars[each]['pb']

        state=True
        while state:
            if self.pBars[each]['value']>=100: state=False
            for i in range(12):
                self.pBars[each]['value']+=10
                print '\n\t ...Working on', each, self.pBars[each]['value'], '\n'
                # pb.update_bar( self.pBars[each]['value'] )
                time.sleep(0.5)

        print "\n\t ProgressBar", each, 'has reached 100%. Its value =', self.pBars[each]['value'], '\n'


    def OK(self):
        for each in self.pBars:
            self.myEvent=threading.Event()
            self.c_thread=threading.Thread(target=self.internalFunc, args=(self.myEvent, each,))
            self.c_thread.start() 


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.resize(480, 320)
    window.show()
    sys.exit(app.exec_())

0 个答案:

没有答案