【问题标题】:PyGTK ProgressBar to display something before subprocess startsPyGTK ProgressBar 在子进程启动之前显示一些东西
【发布时间】:2016-10-27 00:52:30
【问题描述】:

当我点击一个按钮时,我试图让gtk.ProgressBar.set_text('Text') 工作,启动我的subprocess

这是我的代码(完整源代码here):

def on_button_clicked(self, button, progress_bar, filename):
  self.execute(progress_bar, filename)

def execute(self, progress_bar, filename):
  progress_bar.set_text('Encoding')
  progress_bar.pulse()

  cmd = ['ffmpeg', '-y',
         '-i', filename,
         '-r', '30',
         '/tmp/test-encode.mkv']

  process = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE)

  process.wait()

  progress_bar.set_text('Done')

我尝试将progress_bar.set_text('Encoding') 移动到on_button_clicked() 中,但它并没有改变任何东西:我单击按钮,工作完成(文件已​​正式生成并确定),然后进度条才显示“完成”。

我做了功课并阅读了所有相关问题,但它们要么不使用subprocess,要么解析“常规”命令输出。

【问题讨论】:

  • process.wait() 很可能会阻止 GUI 刷新。请改用waitAsync
  • 这个 waitAsync 函数记录在哪里(如果可能,在 python 上下文中)?我似乎必须使用 process.wait (一种子流程方法)才能使整个工作正常;我可以补充一下,它怎么能在它开始之前阻止任何东西之前?因为,好吧,即使进度条没有跳动,我至少应该看到进度条“编码”标签..?我不认为这是一个 GUI 刷新问题,恕我直言。
  • This answer 帮助我摆脱了 sp.wait() 所以现在,PB 正好相反:p "Encoding" does 出现,但 ffmpeg 仍然存在工作完成后“卡住”(文件生成并确定)并且“完成”从不显示:|越来越近了,我猜
  • 所以我想,至少在我的实现中,没有办法知道 ffmpeg 何时结束。好吧,至少这里没有人知道如何:p

标签: ffmpeg subprocess pygtk


【解决方案1】:

只需添加

progress_bar.set_text('Encoding')

while gtk.events_pending():     #   this forces GTK to refresh the screen
    gtk.main_iteration()

on_button_clicked() 中强制 GTK 在继续之前刷新屏幕。

你甚至可以添加

progress_bar.set_text('Finished')
while gtk.events_pending():     #   this forces GTK to refresh the screen
    gtk.main_iteration()

文件写入后在execute 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 2014-10-01
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    相关资源
    最近更新 更多