472,983 Members | 2,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,983 software developers and data experts.

Making a Progress Bar work with a subprocess

How can I make a Progress Bar in Python (GTK+) work together with a subprocess (lets say....youtube-dl or wget )?
Oct 5 '07 #1
1 7586
I was just struggling with this myself, so I happen to have example code for a progressbar. In my case the subprocess is run as a thread within the same program, which makes knowing the relative progress of the work easier.

Expand|Select|Wrap|Line Numbers
  1. import gobject
  2. import gtk
  3. import threading
  4. import time
  5.  
  6. class TestThread(threading.Thread):
  7.     def __init__(self, mainview):
  8.         threading.Thread.__init__(self)
  9.         self.mainview = mainview
  10.  
  11.     def run(self):
  12.         self.work_complete = False
  13.         self.amount_completed = 0
  14.         gobject.timeout_add(100, self._update_bar)
  15.  
  16.         for i in range(10):
  17.             time.sleep(0.3)
  18.             self.amount_completed += .1
  19.  
  20.         self.work_complete = True
  21.  
  22.     def _update_bar(self):
  23.         self.mainview.progressbar.set_fraction(self.amount_completed)
  24.         if self.work_complete:
  25.             self.mainview.progressbar.set_text("Complete!")
  26.         else:
  27.             self.mainview.progressbar.set_text("%d%%" % (self.amount_completed * 100))            
  28.         return not self.work_complete
  29.  
  30. class MainView(gtk.Window):
  31.     def __init__(self):
  32.         gtk.Window.__init__(self)
  33.         self.connect('delete_event', self.handle_window_delete_event)
  34.         self.connect('destroy', self.quit)
  35.  
  36.         self.progressbar = gtk.ProgressBar()
  37.         button1 = gtk.Button("Test")
  38.         button1.connect('clicked', self.button1_click)
  39.         box = gtk.VBox()
  40.         box.pack_start(self.progressbar)
  41.         box.pack_start(button1)
  42.         self.add(box)
  43.  
  44.     def quit(self, *args):
  45.         gtk.main_quit()
  46.  
  47.     def handle_window_delete_event(self, *args):
  48.         return False
  49.  
  50.     def button1_click(self, *args):
  51.         self.progressbar.set_fraction(0)
  52.         worker = TestThread(self)
  53.         worker.start()
  54.  
  55. if __name__ == "__main__":
  56.     gobject.threads_init()
  57.     MainView().show_all()
  58.     gtk.main()
  59.  
Here's a summary of the important aspects:
  • gobject.threads_init() must be called if the application will use other threads
  • The TestThread.run() method is run its own thread and it's not recommended to manipulate widgets directly from there.
  • The TestThread.run() uses gobject.timeout_add() to register a callback method, which will be allowed to freely manipulate widgets and other GTK elements. The method will be called automatically with 100 millisecond intervalls. By returning False the callback method will be disabled.
  • Obviously you have to have some way to measure the overall progress to show relevant "percent completed" info. You should calculate the fraction of downloaded bytes per total amount of bytes and assign it to the ProgressBar using ProgressBar.set_fraction(). If you cant know the relative progress I suggest you show a pendling progress bar without percentage info until the download hads completed.

I verified that the ProgressBar may be manipulated directly from the TestThread. However, I have had problems (segfaults, deadlocks, etc.) if widget manipulations have been performed from other threads than the GTK main thread.
Oct 5 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Miki Tebeka | last post by:
Hello All, Any ideas on how to show subprocess progress? I have a GUI application that launches a subprocess which does some lengthy work. What is a good way for the two to communicate so...
0
by: Roman Neuhauser | last post by:
Hello, I have a piece of code that gets run in a script that has its stdout closed: import sys sys.stdout = sys.stderr c = subprocess.Popen (..., stdin = subprocess.PIPE,
4
by: Marc Carter | last post by:
I am trying to rewrite a PERL automation which started a "monitoring" application on many machines, via RSH, and then multiplexed their collective outputs to stdout. In production there are lots...
2
by: Stewart Midwinter | last post by:
this has me puzzled; I've created a small test app to show the problem I'm having. I want to use subprocess to execute system commands from inside a Tkinter app running under Cygwin. When I...
3
by: Darren Dale | last post by:
I'm a developer on the matplotlib project, and I am having trouble with the subprocess module on windows (Python 2.4.2 on winXP). No trouble to report with linux. I need to use _subprocess instead...
3
by: madpython | last post by:
playing with subprocess.Popen on Windows I stumbled into the following problem: Python 2.4.3 (#69, Mar 29 2006, 17:35:34) IDLE 1.1.3 >>> import subprocess >>>...
5
by: Cameron Laird | last post by:
Question: import subprocess, StringIO input = StringIO.StringIO("abcdefgh\nabc\n") # I don't know of a compact, evocative, and # cross-platform way to exhibit this behavior. # For now, depend...
9
by: Phoe6 | last post by:
Hi all, Consider this scenario, where in I need to use subprocess to execute a command like 'ping 127.0.0.1' which will have a continuous non- terminating output in Linux. # code # This...
12
by: bhunter | last post by:
Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.