我正在尝试使用PyQt为名为“HandBrake”的视频转换器应用程序构建一个简单的gui。
我的问题是,当我选择要转换的视频文件时,子进程Popen启动手刹应用程序并使用必要的args但在等待手刹完成时gui被阻止,所以我无法做任何更改。 (例如:我无法禁用pushButton也不能更改其文本)
我不是在寻找更复杂的解决方案,例如进度条等。但我想在等待程序完成转换时,只需禁用按钮并更改其文本。
我怎样才能用python& amp; 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
答案 0 :(得分: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)
[...]
答案 1 :(得分:1)
如果你不关心输出,你可以使用p.wait()等待subproc完成,但你仍然需要将控制返回到QT主循环,所以你需要插入一个线程不知何故。最简单的解决方案是:
import threading
def reenable():
p.wait()
self.pushButton.setEnabled(1)
t = threading.Thread(reenable)
t.run()
这有很多未解决的问题。例如,是否允许从多个线程调用GUI操作?超时怎么样?但它应该足以指出你正确的方向。