PyGTK ProgressBar to display something before subprocess starts

▼魔方 西西 提交于 2019-12-12 05:26:04

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/38012773/pygtk-progressbar-to-display-something-before-subprocess-starts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!