2014-09-22 114 views
0

我想用一個任務的進度更新主窗口上的進度條,我正在對另一個子例程進行操作,是否有可能?從子例程更新進度條

要儘可能明確,我將有2個文件:

在我Mainwindow.py我會是這樣的:

import Calculations 

#some code 
self.ui.progressBar 
Calculations.longIteration("parameters") 

然後我會爲一個單獨的文件計算:Calculations.py

def longIteration("parameters") 

#some code for the loop 

"here I would have a loop running" 
"And I would like to update the progressBar in Mainwindow" 

這可能嗎?

或者它應該以不同的方式完成?

謝謝。

回答

1

最簡單的方法是簡單地通過所述GUI對象:

self.ui.progressBar 
Calculations.longIteration("parameters", self.ui.progressBar) 

Calculations更新progressBar。這有兩個問題,雖然:

  • 你混合GUI代碼Calculations,誰可能不應該知道它
  • 如果longIteration是一個長期運行的功能,如它的名字所暗示的,你是什麼阻止你的GUI主線程,這將使許多GUI框架不愉快(和你的應用程序無響應)。

另一種解決方案是在一個線程中運行longIteration,並通過您用來更新進度條的回調函數:

import threading 
def progress_callback(): 
    #update progress bar here 
threading.Thread(target=Calculations.longIteration, args=["parameters", progress_callback]).run() 

然後,裏面longIteration,千萬:

def longIteration(parameters, progress_callback): 
    #do your calculations 
    progress_callback() #call the callback to notify of progress 

你可以修改progress_callback,如果你需要它們,你可以參數很明顯

+0

嗨Goncalopp,謝謝fo你的答案。你對第一種選擇是正確的。我採取了第一個選項,因爲它對我來說更容易,但是,是的,MainWindow變得沒有響應,所以progressBar不會更新直到循環結束。所以我的問題與你的第二個選擇,與線程的問題是,我不完全知道如何更新progressBar與循環的進程從線程 – codeKiller 2014-09-22 11:58:47

+1

@ newPyUser您需要調用'longIteration'回調。我已經編輯了這個問題,以明確 – goncalopp 2014-09-22 13:10:00

+0

再次感謝,我會盡力使它工作,看起來不錯! – codeKiller 2014-09-22 13:17:44