子进程Popen阻塞PyQt GUI

eym*_*men 1 python user-interface subprocess pyqt popen

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

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

我不是在寻找更复杂的解决方案,比如进度条等.但我想在等待程序完成转换时,只需禁用按钮并更改其文本.

我怎么能用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
Run Code Online (Sandbox Code Playgroud)

auk*_*ost 10

因为你已经在Qt的土地,你可以做这样的事情:

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)

    [...]
Run Code Online (Sandbox Code Playgroud)

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