Skip to content Skip to sidebar Skip to footer

Subprocess Popen Blocking Pyqt Gui

I'm trying to build a simple gui for a video converter application called 'HandBrake' using PyQt. My problem is that when I choose a video file to convert, subprocess Popen starts

Solution 1:

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

from PyQt4.QtCore import QProcess

classYourClass(QObject):

    [...]

    defvideoProcess(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)

    defonFinished(self, exitCode, exitStatus):
        self.pushButton.setEnabled(True)

    [...]

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

Solution 2:

If you don't care about the output anyway, you can use p.wait() to wait for the subproc to finish, but you still need to return control to the QT main loop, so you need to insert a thread somehow. The simplest solution would be something like:

import threading
def reenable():
    p.wait()
    self.pushButton.setEnabled(1)
t = threading.Thread(reenable)
t.run()

There are numerous unresolved issues with this. For example, is it permissible to call GUI actions from multiple thread? What about a timeout? But it should be enough to point you in the right direction.

Post a Comment for "Subprocess Popen Blocking Pyqt Gui"