PySide/Python GUI冻结 [英] PySide / Python GUI freezes

查看:118
本文介绍了PySide/Python GUI冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为rvplayer编写GUI,使艺术家能够自动渲染带有板岩和老化信息的日报. GUI使用PySide编写,并使用Python 2.7编写脚本.我的问题是,在调用我的进程并使用标准输出更新我的QProgressBar时,GUI冻结了.我知道这是一个常见问题,可能可以通过processEvents()解决,但是我对线程和进程循环了解得很少,因此无法解决这个问题.由于我的代码已经有点冗长,因此,这是导致问题的部分:

I'm currently writing a GUI for rvplayer that shall enable artists to automatically render dailies with slate and burn-in information. The GUI is written with PySide and scripted in Python 2.7. My problem is that upon calling my process and updating my QProgressBar with the stdout the GUI freezes. I know that this is a common problem and that it can probably be solved with processEvents() somehow, but I know far too little about threading and process loops to get my head around this issue. Since my code is a little lengthy already, here's the part that causes the issue:

def rv(self, args):
    p = subprocess.Popen(["C:/Program Files/Tweak/RV-4.0.10-64/bin/rvio_hw.exe"]+[x for x in args], stdout=subprocess.PIPE)
    while True:
        line = p.stdout.readline()
        if line != "":
            progressStr=re.search(r"([0-9]+.[0-9]+%)", line.rstrip())
            if progressStr == None:
                print line.rstrip()
            else:
                progressInt=int(float(re.sub("[^0123456789\.]", "", progressStr.group())))
                self.prog_QProgressBar.setValue(progressInt)
                print progressStr.group()
        else:       
            break

这是开始我的QApplication的部分:

if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    finalForm = MainWindow()
    finalForm.show()
    sys.exit(app.exec_())

按下按钮时,我正在调用rv函数,尽管进度条保持正常更新,但一段时间后窗口开始变得无响应.我不知道在什么时候可以使用app.processEvents()告诉QApplication在单独的线程中或在后台运行该进程.

I'm calling the function rv upon pressing a button and although the progress bar keeps updating normally, the window starts to get nonresponsive after some time. I do not understand at which point I could use app.processEvents() to tell my QApplication to run the process in a separate thread or in the background.

推荐答案

由于您似乎没有使用线程,因此可能所需要做的就是在更新进度条后调用processEvents,如下所示:

Since it looks like you're not using threads, it may be that all that's required is to call processEvents after updating the progress bar, like this:

    self.prog_QProgressBar.setValue(progressInt)
    QtGui.qApp.processEvents()

但是,其有效性可能取决于过程产生输出所花费的时间. processEvents调用的全部作用是立即处理当前在应用程序的事件队列中的所有未决事件(例如,小部件绘制,鼠标单击等).在这两次调用之间,GUI将继续冻结(即,执行的代码未按照您的建议在单独的线程中或在后台运行).因此,该技术可以使GUI保持响应的程度取决于在rv()方法中调用processEvents的频率.

However, the effectiveness of this may depend on how long it takes the process to produce the output. All that the processEvents call does is to immediately handle any pending events (e.g. widget painting, mouse clicks, etc) that are currently in the application's event queue. In between those calls, the GUI will continue to freeze (i.e. the executing code is not run in a separate thread or in the background, as you suggested). So the extent to which this technique can keep the GUI responsive depends on how frequently processEvents can be called within the rv() method.

这篇关于PySide/Python GUI冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆