子进程Popen阻止PyQt GUI [英] subprocess Popen blocking PyQt GUI

查看:99
本文介绍了子进程Popen阻止PyQt GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PyQt为视频转换器应用程序"HandBrake"构建一个简单的GUI.

I'm trying to build a simple gui for a video converter application called "HandBrake" using PyQt.

我的问题是,当我选择要转换的视频文件时,子进程Popen使用必要的args启动手刹应用程序,但是在等待手刹完成gui时被阻止,因此我无法进行任何更改. (例如:我不能禁用按钮,也不能更改其文本)

My problem is that when I choose a video file to convert, subprocess Popen starts handbrake application with the necessary args but while waiting for handbrake to finish the gui gets blocked so I can't do any changes. (Ex: I can't disable the pushButton nor change its text)

我不是在寻找更复杂的解决方案,例如progressbar等,但是我想在等待程序完成转换的同时简单地禁用该按钮并更改其文本.

I'm not looking for a more complicated solution such as progressbar etc. but I'd like to simply disable the button and change its text while waiting for the program to finish converting.

我该如何使用python& pyqt?

How can I do such thing with python & pyqt?

def videoProcess():
    self.pushButton.setEnabled(0)
    self.pushButton.setText("Please Wait")
    command = "handbrake.exe -i somefile.wmv -o somefile.mp4"
    p = subprocess.Popen(str(command), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    while 1:
        line = p.stdout.readline()
        if not line:
            self.pushButton.setEnabled(1)
            break

推荐答案

由于您位于Qt国土,因此您可以执行以下操作:

Since you are in Qt land already you could do something like this:

from PyQt4.QtCore import QProcess

class YourClass(QObject):

    [...]

    def videoProcess(self):
        self.pushButton.setEnabled(0)
        self.pushButton.setText("Please Wait")
        command = "handbrake.exe"
        args =  ["-i", "somefile.wmv", "-o", "somefile.mp4"]
        process = QProcess(self)
        process.finished.connect(self.onFinished)
        process.startDetached(command, args)

    def onFinished(self, exitCode, exitStatus):
        self.pushButton.setEnabled(True)

    [...]

http://doc.qt.io/qt-5/qprocess.html

这篇关于子进程Popen阻止PyQt GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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