Advertisement
3nids

progress bar

Jun 24th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1.  
  2.  
  3. import sys
  4. from time import sleep
  5.  
  6. from PyQt4.QtCore import *
  7. from PyQt4.QtGui import *
  8.  
  9. mainLoopCount = 10
  10. innerLoopCount = 1000
  11. durationInnerLoop = 4.0 # seconds (float!)
  12.  
  13.  
  14.  
  15. class dialog(QDialog):
  16.     progress = 0
  17.     def __init__(self):
  18.         QDialog.__init__(self)
  19.         layout = QGridLayout(self)
  20.         self.progressBar = QProgressBar(self)
  21.         layout.addWidget(self.progressBar,0,0)
  22.         button = QPushButton("start")
  23.         button.clicked.connect(self.start)
  24.         layout.addWidget(button,0,1)
  25.         self.progressBar.setMinimum(0)
  26.         self.progressBar.setMaximum(mainLoopCount)
  27.                
  28.     def start(self):
  29.         for i in range(0,10):
  30.             QCoreApplication.processEvents()
  31.             self.progress = i
  32.             self.setProgress()
  33.             worker = Worker()
  34.             worker.progress.connect(self.setProgress)
  35.             worker.work()
  36.            
  37.     def setProgress(self,value=0):
  38.         p = self.progress + float(value) / innerLoopCount
  39.         print p
  40.         self.progressBar.setValue(p)
  41.        
  42.        
  43.        
  44. class Worker(QObject):
  45.     progress = pyqtSignal(int)
  46.    
  47.     def __init__(self):
  48.         QObject.__init__(self)
  49.        
  50.     def work(self):
  51.         for i in range(0,1000):
  52.             QCoreApplication.processEvents()
  53.             sleep(durationInnerLoop/innerLoopCount)
  54.             self.progress.emit(i)
  55.  
  56.  
  57.  
  58. if __name__ == "__main__":
  59.     app = QApplication(sys.argv)
  60.     main_window = dialog()
  61.     main_window.show()
  62.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement