2014-10-10 77 views
1

我有一段代码,我在其他地方使用过,但在这种情况下,它没有。基本上我的函数体计算了一堆东西,将结果存入一个pandas DataFrame并将其绘制到一个matplotlib画布上。PyQt:QProgressDialog显示但是为空(显示背景内容)

我把下面的代码放在函数体的顶部,希望它在函数调用期间显示一个对话框。但是,所发生的一切都是出现对话框,其中框内容反映了屏幕后立即显示的内容。这将在函数调用期间保留在屏幕上,然后在完成运行时正确关闭。

任何想法出了什么问题?

万一它有用,父母(self)是QWidget

噢,还有一件事,如果我删除了progress.cancel()行,一旦该函数的主要部分完成执行,QProgressDialog实际上会使用进度栏和标签文本('Updating ...')自己绘制自己。

非常感谢您的帮助!

progress = QProgressDialog(parent = self) 
progress.setCancelButton(None) 
progress.setLabelText('Updating...') 
progress.setMinimum(0) 
progress.setMaximum(0) 
progress.forceShow() 

#calculate and plot DataFrame 

progress.cancel() 
+1

你可以尝试app.processEvents (),其中app是QApplication实例,在进入计算之前。 – mdurant 2014-10-10 15:54:59

+0

感谢您的回复!有没有一种简单的方法可以从我的课程中访问应用程序实例? – Ben 2014-10-10 17:22:33

+2

'QtGui.QApplication.instance()' – mdurant 2014-10-10 17:24:41

回答

0

我发现有同样的问题在某些时候自己与这个装饰,我申请,我想运行闭锁功能上来,同时允许GUI线程来处理事件。

def nongui(fun): 
    """Decorator running the function in non-gui thread while 
    processing the gui events.""" 
    from multiprocessing.pool import ThreadPool 
    from PyQt4.QtGui import QApplication 

    def wrap(*args, **kwargs): 
     pool = ThreadPool(processes=1) 
     async = pool.apply_async(fun, args, kwargs) 
     while not async.ready(): 
      async.wait(0.01) 
      QApplication.processEvents() 
     return async.get() 

    return wrap 

然后,它很容易与装饰通常写你的计算功能:

@nongui 
def work(input): 
    # Here you calculate the output and set the 
    # progress dialog value 
    return out 

,然后运行它像往常一样:

out = work(input) 
0
progress = QProgressDialog(parent = self) 
progress.setCancelButton(None) 
progress.setLabelText('Updating...') 
progress.setMinimum(0) 
progress.setMaximum(10) 
progress.forceShow() 

progress.setValue(0) 

#calculate and plot DataFrame 
domystuff(progress) # pass progress to your function so you can continue to increment progress 

qApp.processEvents() # qApp is a reference to QApplication instance, available within QtGui module. Can use this whereever you are in your code, including lines in your domystuff() function. 


progress.setValue(10) # progress bar moves to 100% (based on our maximum of 10 above) 



progress.done() # I don't think you want to cancel. This is inherited from QDialog 
+0

再读一遍之后,无论何时调用setValue(),processEvents()也会被调用。当调用setValue(max)时,对话完成并退出。 progress.done()实际上并不需要。 – cmoman 2015-12-16 22:05:39