问题
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