Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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/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
Python 子进程Popen阻塞PyQt GUI_Python_User Interface_Pyqt_Subprocess_Popen - Fatal编程技术网

Python 子进程Popen阻塞PyQt GUI

Python 子进程Popen阻塞PyQt GUI,python,user-interface,pyqt,subprocess,popen,Python,User Interface,Pyqt,Subprocess,Popen,我正在尝试使用PyQt为名为“HandBrake”的视频转换器应用程序构建一个简单的gui 我的问题是,当我选择要转换的视频文件时,subprocess Popen使用必要的参数启动handbrake应用程序,但在等待handbrake完成gui时,gui被阻止,因此我无法进行任何更改。(例如:我无法禁用按钮或更改其文本) 我不是在寻找一个更复杂的解决方案,如progressbar等,但我想简单地禁用按钮并在等待程序完成转换时更改其文本 我如何使用python和pyqt做这样的事情 def vi

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

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

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

我如何使用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

如果您无论如何都不关心输出,那么可以使用p.wait()等待子程序完成,但是您仍然需要将控制权返回到QT主循环,因此您需要以某种方式插入一个线程。最简单的解决方案如下:

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

这方面有许多悬而未决的问题。例如,是否允许从多个线程调用GUI操作?暂停一下怎么样?但这应该足以为您指明正确的方向。

既然您已经在Qt land,您可以这样做:

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)

    [...]

使用单独的线程或使用
多处理
。感谢您的回答,但我想我忘了提及我可以在处理后重新启用按钮,但我无法在开始时禁用它,尽管我在popen之前调用了“self.butdown.setEnabled(0)”函数。