A simple Progressbar implementation in Python

by José Antonio Oliveira

A quick post just to share a small class to display a Progressbar on Canvas:

Usage example:

#buffer could be a copy of canvas
progress = ProgressBar(buffer)
i = 0
for i in range(100):
    progress.set_value(i)
    canvas.blit(buffer)
progress.close()
del progress

I made four screenshots of this progressbar running inside an application:
screenshot0052

screenshot0053
screenshot0054
screenshot0057

Here is the complete ProgressBar class:

from graphics import *
 
class ProgressBar(object):
    """ Implements a ProgressBar on Canvas
    """
    def __init__(self, other_canvas, start=0, end=100,
                 color=(0,0,77), fill=(255,255,200),
                 outline=(0,0,0)):
        #canvas assignments
        self.canvas_copy = Image.new(other_canvas.size)
        self.canvas_copy.blit(other_canvas)
        self.return_canvas = Image.new(self.canvas_copy.size)
        self.return_canvas.blit(other_canvas)
        self.canvas = other_canvas
        #External box size
        self.box_w = int(self.canvas_copy.size[0] * 0.8)
        self.box_h = 50 #height of window
        self.box_l = int(self.canvas_copy.size[0] - self.box_w) / 2
        self.box_t = self.canvas_copy.size[1] - self.box_h - 5
        #ProgressBar size
        self.progr_margin_h = 5 #horizontal margins (left and right)
        self.prog_w = self.box_w - 2 * self.progr_margin_h
        self.prog_h = 18 #height of progressbar
        self.prog_l = self.box_l + self.progr_margin_h
        self.prog_t = self.box_t + int((self.box_h - self.prog_h) / 2)
        #internal progressbar expects that external has 1px border
        self.internal_w_max = self.prog_w - 2
        self.internal_h = self.prog_h - 2
        self.internal_l = self.prog_l + 1
        self.internal_t = self.prog_t + 1
        self.internal_w = 0
        #colors & values
        self.start = start
        self.end = end
        self.value = start
        self.color = color
        self.outline = outline
        self.fill = fill
        #shows initial progressbar
        self.redraw()
 
    def close(self):
        #Closes the window and frees the image buffers memory
        self.canvas.blit(self.return_canvas)
        del self.canvas_copy
        del self.return_canvas
 
    def set_value(self, value):
        if value > self.end:
            value = self.end
        elif value < self.start:
            value = self.start
        self.value = value
        self.internal_w = int(((1.0 * self.value - self.start)/ \
                               (1.0 * self.end - self.start))   \
                              * self.internal_w_max)
        self.redraw()
 
    def redraw(self):
        """You don't need call redraw on application.
        Just use set_value to redraw the progressbar"""
        self.canvas_copy.blit(self.return_canvas)
        #external window
        self.canvas_copy.rectangle((self.box_l,
                                    self.box_t,
                                    self.box_l + self.box_w,
                                    self.box_t + self.box_h),
                                   outline=self.outline,
                                   fill=self.fill)
        #progressbar external border
        self.canvas_copy.rectangle((self.prog_l,
                                    self.prog_t,
                                    self.prog_l + self.prog_w,
                                    self.prog_t + self.prog_h),
                                   outline=self.outline,
                                   fill=self.fill)
        #progressbar core
        self.canvas_copy.rectangle((self.internal_l,
                                    self.internal_t,
                                    self.internal_l + self.internal_w,
                                    self.internal_t + self.internal_h),
                                   outline=None,
                                   fill=self.color)
        self.canvas.blit(self.canvas_copy)

The next post will be the implementation of this progressbar using Topwindow

Related posts:

  1. A simple Progressbar implementation in Python (Part II) Following the last post, here’s the other implementation of ProgressBar,...
  2. Unleash the power of PNG transparency with masks Although PyS60 is not able to deal with images with...
  3. Working with math sets using Python I always try to bring what I learn into programming,...

Related posts brought to you by Yet Another Related Posts Plugin.