Friday, July 20, 2018

PyGTK - ProgressBar Class

PyGTK - ProgressBar Class


 Progress bars are used to give user the visual indication of a long running process. The gtk.ProgressBar widget can be used in two modes — percentage mode and activity mode.
When it is possible to accurately estimate how much of work is pending to be completed, the progress bar can be used in percentage mode, and the user sees an incremental bar showing percentage of completed job. If on the other hand, the amount of work to be completed can be accurately determined, the progress bar is used in activity mode in which, the bar shows the activity by displaying a block moving back and forth.
The following constructor initializes the widget of the gtk.ProgressBar class −
pb = gtk.ProgressBar()
gtk.ProgressBar uses the following methods to manage functionality −
  • ProgressBar.pulse() − This nudges the progressbar to indicate that some progress has been made, but you don't know how much. This method also changes the progress bar mode to "activity mode," where a block bounces back and forth.
  • ProgressBar.set_fraction(fraction) − This causes the progress bar to "fill in" the portion of the bar specified by fraction. The value of fraction should be between 0.0 and 1.0.
  • ProgressBar.set_pulse_setup() − This sets the portion (specified by fraction) of the total progress bar length to move the bouncing block for each call to the pulse() method.
  • ProgressBar.set_orientation() − This sets the orientation of the progress bar. It may be set to one of the following constants:
    • gtk.PROGRESS_LEFT_TO_RIGHT
    • gtk.PROGRESS_RIGHT_TO_LEFT
    • gtk.PROGRESS_BOTTOM_TO_TOP
    • gtk.PROGRESS_TOP_TO_BOTTOM
In the following program, the gtk.ProgressBar widget is used in activity mode. Hence, the initial position of progress is set to 0.0 by the set_fraction()method.
self.pb = gtk.ProgressBar()
self.pb.set_text("Progress")
self.pb.set_fraction(0.0)
In order to increment the progress by 1 percent after 100 milliseconds, a timer object is declared and a callback function is set up to be invoked after every 100 ms so that the progress bar is updated.
self.timer = gobject.timeout_add (100, progress_timeout, self)
Here, progress_timeout() is the callback function. It increments the parameter of the set_fraction() method by 1 percent and updates the text in progress bar to show the percentage of completion.
def progress_timeout(pbobj):
new_val = pbobj.pb.get_fraction() + 0.01
pbobj.pb.set_fraction(new_val)
pbobj.pb.set_text(str(new_val*100)+" % completed")
return True

Example

Observe the following code −
import gtk, gobject
   def progress_timeout(pbobj):
      new_val = pbobj.pb.get_fraction() + 0.01
      pbobj.pb.set_fraction(new_val)
      pbobj.pb.set_text(str(new_val*100)+" % completed")
      return True

class PyApp(gtk.Window):
   def __init__(self):
      super(PyApp, self).__init__()
      self.set_title("Progressbar demo")
      self.set_size_request(300,200)
      self.set_position(gtk.WIN_POS_CENTER)
  
      fix = gtk.Fixed()
      self.pb = gtk.ProgressBar()
      self.pb.set_text("Progress")
      self.pb.set_fraction(0.0)
  
      fix.put(self.pb,80,100)
      self.add(fix)
      self.timer = gobject.timeout_add (100, progress_timeout, self)
      self.connect("destroy", gtk.main_quit)
      self.show_all()
PyApp()
gtk.main()
The above code will generate the following output −
ProgressBar Demo
To use the progress bar in activity mode, change callback function to the following and run −
def progress_timeout(pbobj):
pbobj.pb.pulse()
return True
The back and forth movement of a block inside the Progress bar will show the progress of the activity.
ProgressBar Demo

No comments:

Post a Comment