PyGTK ProgressBar在子进程启动前显示内容 [英] PyGTK ProgressBar to display something before subprocess starts

查看:160
本文介绍了PyGTK ProgressBar在子进程启动前显示内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击之前的按钮时,我试图获得 gtk.ProgressBar.set_text('Text')启动我的子流程



这是我的代码(全源代码 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' ,文件名,
'-r','30',
'/tmp/test-encode.mkv']

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

process.wait()

progress_bar.set_text('Done')
/ pre>

我尝试移动 progress_bar.set_text('Encoding')但是它并没有改变任何东西:我点击按钮,工作已经完成(该文件已正确生成并确定),只有在进度条上说完成。



我做了我的家庭作业,并阅读了所有相关问题,但他们不要使用 subprocess 或解析常规命令输出。

解决方案

只需添加

  progress_bar.set_text('Encoding')

while gtk.events_pending( ):#这会强制GTK刷新屏幕
gtk.main_iteration()

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



您甚至可以添加

  progress_bar.set_text('Finished')
而gtk.events_pending():#这将强制GTK刷新屏幕
gtk .main_iteration()
I'm trying to get gtk.ProgressBar.set_text('Text') to work when I click on a button, prior to launch my subprocess.

Here is my code (full source 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')

I tried moving progress_bar.set_text('Encoding') in on_button_clicked() but it doesn't change anything: I click the button, the job is done (the file is duly produced and OK) and only then the progress bar says "Done".

I did my homework and read all related questions, but they either don't use subprocess, or parse "regular" command outputs.

解决方案

Just add

progress_bar.set_text('Encoding')

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

in on_button_clicked() to force GTK to refresh the screen before continuing.

You could even add

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

in execute once the file is written.

这篇关于PyGTK ProgressBar在子进程启动前显示内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆