0

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.

yPhil
  • 8,049
  • 4
  • 57
  • 83
  • `process.wait()` is most probably blocking GUI refreshes. Try with `waitAsync` instead. – Ilya Jun 24 '16 at 12:36
  • Where is this waitAsync function documented (in a python context, if possible)? I seem to have to use process.wait (a subprocess method) to get the whole thing to work ; I might add, how can it block anything *before* it even starts? Because, well, I should at least see the progressbar "Encoding" label even if the bar doesn't pulsate..? I don't think this is a GUI refresh problem, IMHO. – yPhil Jun 24 '16 at 12:47
  • http://api.gtkd.org/src/gio/Subprocess.html – Ilya Jun 24 '16 at 13:11
  • [This answer](http://stackoverflow.com/a/35199136/1729094) helped me get rid of sp.wait() so now, the PB is the other way around :p "Encoding" *does* show up, but ffmpeg stays "stuck" after the job is done (file is produced & OK) and "Done" never shows :| getting closer ever minute, I guess – yPhil Jun 24 '16 at 13:18
  • So I guess, at least w/ my implementation, there is no way to know when ffmpeg has ended. Well, at least nobody here knows how :p – yPhil Jun 24 '16 at 13:46
  • Can't you set a callback on end of process ? – Ilya Jun 24 '16 at 13:51
  • Ha! I whish I could do that. A callback is **exactly** what I need. And [no, it doesn't look that way](https://docs.python.org/2/library/subprocess.html#subprocess.Popen) – yPhil Jun 24 '16 at 13:57
  • ...or monitor what the process writes in stdout ? – Ilya Jun 24 '16 at 13:58

1 Answers1

0

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.

zondo
  • 19,901
  • 8
  • 44
  • 83
Louis
  • 2,854
  • 2
  • 19
  • 24