subprocess Popen blocking PyQt GUI(子进程 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:子进程 Popen 阻塞 PyQt GUI
基础教程推荐
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
