2014-09-22 55 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