Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何确保PyQt中显示QProgressDialog_Python_User Interface_Pyqt_Pyside_Qprogressbar - Fatal编程技术网

Python 如何确保PyQt中显示QProgressDialog

Python 如何确保PyQt中显示QProgressDialog,python,user-interface,pyqt,pyside,qprogressbar,Python,User Interface,Pyqt,Pyside,Qprogressbar,有时我的QProgressDialog会显示,有时根本不会显示(就好像没有调用processEvents一样)。processEvents()命令是否存在任何可能导致QProgressDialog在某些情况下不显示的工件 我的问题是一般性的,因为我还不能在代码中隔离问题。但是,我注意到,当我的QProgressDialog没有显示时,它发生在我使用配置解析器访问文本文件时。解决方法是在文件关闭后执行time.sleep()(可能是为了确保流程完成,然后processEvents将开始显示QPro

有时我的QProgressDialog会显示,有时根本不会显示(就好像没有调用processEvents一样)。processEvents()命令是否存在任何可能导致QProgressDialog在某些情况下不显示的工件

我的问题是一般性的,因为我还不能在代码中隔离问题。但是,我注意到,当我的QProgressDialog没有显示时,它发生在我使用配置解析器访问文本文件时。解决方法是在文件关闭后执行time.sleep()(可能是为了确保流程完成,然后processEvents将开始显示QProgressDialog)

如果有帮助,以下是我作为生成器运行QProgressDialog的代码:

def progress_dialog(self, data, label, window_title, stop_label, capture_bar=False):
    bar = QProgressDialog(label, stop_label, 0, len(data))
    if capture_bar: self.prog_bar = bar
    bar.setWindowTitle(window_title)
    for k, d in enumerate(data):
        QCoreApplication.instance().processEvents()
        if bar.wasCanceled():
            raise GeneratorExit
        # set the next value beyond the start of 0
        bar.setValue(k+1)
        # again process events to draw the new label and value
        QCoreApplication.instance().processEvents()
        yield(d)
        raise StopIteration
再一次,很抱歉,我没有这个孤立问题的完整代码片段(完整的代码太大了)。我想我要找的是一个为什么要检查processEvents()命令是否正在执行它的任务(因为我显然在调用它,但它挂起在其他进程上,而不是显示对话框)

编辑:


根据此支持请求,执行“bar.show()”命令将强制显示进度条


我将等待几周,并确保这是一个有保证的修复,然后再将其作为答案发布。

如果您需要显示
QprogesDialog
,而不考虑过程的持续时间,请使用其值为0的方法。根据文档,默认最小值为4000ms。

根据此支持请求,执行
条。show()
命令将强制进度条显示:

只需在每次
processevents
调用之前和首次构建进度条之后调用
show()
方法即可


我已经等了将近4个月,这个解决方案还没有失败。这似乎是一个足够的答案。

这可能是一个旧线程,但我有一个类似的问题,show()使对话框出现,但为空。所以,我提出了这个decorator,我将它应用于我想要运行阻塞的函数,同时允许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
然后,使用decorator很容易正常编写计算函数:

@nongui
def work(input):
    # Here you calculate the output and set the 
    # progress dialog value
    return out
然后像往常一样运行它:

out = work(input)

不幸的是,这并没有解决我的特殊问题。仍然对你的帮助表示+1感谢。