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

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

问题描述

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

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

我的问题是,当我选择要转换的视频文件时,子进程 Popen 会使用必要的参数启动手刹应用程序,但在等待手刹完成时,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)

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

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天全站免登陆