2016-01-24 43 views
0

我正在學習使用pyQt4的GUI python。我在另一個文件python中有功能A。我想運行在我從文件.ui(設計器pyQt4的輸出)中提取的GUI文件python中。如何創建在功能A正在運行時處於活動狀態的活動指示燈?我可以在不知道我的功能A運行多少次的情況下使用進度條(在pyQt4設計器中)?如何使用PyQt4設計器爲python創建活動指示器

謝謝。

這是調用A在GUI的.py功能:

def RunFunction(): 
    import Kdtree 
    _dir = kdTreeOk.getNeighbor(float(radius)) #function 'A' 
    file = file_open('Summary.txt',_dir) # ignore, just file to save result of `A` 
    with file: 
     textOutput=file.read() 
     ui.result.setPlainText(textOutput) 

#### button to run RunFunction in file GUI .py 

ui._run.clicked.connect(RunFunction) 
+0

A'以任何方式與GUI交互? (它是否會調用PyQt函數?) –

+0

我在文件GUI.py中創建函數來調用'A'。然後我連接按鈕以使用該功能運行'A'。 我會寫這個函數來調用'A'。 –

回答

0

QProgressDialog由用於此目的,並且通常經由QThread調用。這裏有一個(混亂的)基本例子來說明這可以如何工作(沒有任何線程)。如果您從另一個窗口調用此對話框,只需將parent設置爲調用窗口,並且您可以通過調用self.parent.some_variable來讀取此對話框中的屬性。

EDITED正常工作;)。

from PyQt4 import QtCore, QtGui 
from time import sleep 
import sys 

class ProgressBarWidget(QtGui.QProgressDialog): 

    def __init__(self, parent=None, app=None): 
     super(ProgressBarWidget, self).__init__(parent) 

     self.app=app 
     self._allow_close = True 

     layout = QtGui.QVBoxLayout(self) 

     # Create a progress bar and a button and add them to the main layout 
     self.progressBar = QtGui.QProgressBar(self) 
     self.progressBar.setRange(0,100) 
     layout.addWidget(self.progressBar) 
     self.button = QtGui.QPushButton("Start", self) 
     layout.addWidget(self.button) 

     self.button.clicked.connect(self.onStart) 

     self.upload_count = 10 


    def onStart(self): 
     self.progressBar.setValue(0) 
     self.button.setText("Uploading...") 
     self.run() 

    def makeProgress(self, current_num, total_num, message = ''): 
     if total_num == current_num: 
      self.onFinished() 

     elif current_num == 0: 
      self.progressBar.setValue(0) 
     else: 
      multiplier = int(float(float(100)/float(total_num))) 
      c_times_m = current_num * multiplier 
      for i in xrange(c_times_m - int(self.progressBar.value())): 
       new_val = int(self.progressBar.value()) + 1 
       self.progressBar.setValue(new_val) 
       sleep(.01) 


    def onFinished(self): 
     # progress complete 
     self.progressBar.setRange(0,100) 
     for i in xrange(int(self.progressBar.value()),101): 
      self.progressBar.setValue(i) 
     self.button.setEnabled(True) 
     self.button.setText('Exit') 
     self.button.clicked.disconnect(self.onStart) 
     self.button.clicked.connect(self.close) 

    def run(self): 
     self._allow_close = False 
     self.button.setDisabled(True) 

     total = self.upload_count * 2 

     progress_meter = 0 

     downloaded = [] 
     tests_to_upload = 10 
     for each in xrange(tests_to_upload): 
      sleep(0.15) 
      progress_meter += 1 
      self.makeProgress(progress_meter,total) 

      sleep(0.2) 
      progress_meter += 1 

      self.makeProgress(progress_meter, total) 

if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    window = ProgressBarWidget(app=app) 
    window.resize(640, 480) 
    window.show() 
    sys.exit(app.exec_()) 
+0

對不起,你能給我一個例子,在我的情況下使用這個類嗎?我是空白的:'( –