Package SimPy :: Module tkprogressbar |
[hide private]
[frames] | no frames]

# Source Code for Module SimPy.tkprogressbar

     1  from Tkinter import *
     2
     3 -class ProgressBar:
     4 -    def __init__(self, master = None, orientation = 'horizontal',
     5                   min = 0, max = 100, width = 100, height = 18,
     6                   doLabel = 1, appearance = 'sunken',
     7                   fillColor = 'blue', background = 'gray',
     8                   labelColor = 'yellow', labelFont = 'Verdana',
     9                   labelText = "', labelFormat = '%d%%",
    10                   value = 50, bd = 2):
    11          # preserve various values
    12          self.master = master
    13          self.orientation = orientation
    14          self.min = min
    15          self.max = max
    16          self.width = width
    17          self.height = height
    18          self.doLabel = doLabel
    19          self.fillColor = fillColor
    20          self.labelFont = labelFont
    21          self.labelColor = labelColor
    22          self.background = background
    23          self.labelText = labelText
    24          self.labelFormat = labelFormat
    25          self.value = value
    26          self.frame = Frame(master, relief = appearance, bd = bd)
    27          self.canvas = Canvas(self.frame, height = height, width = width, bd = 0,
    28                             highlightthickness = 0, background = background)
    29          self.scale = self.canvas.create_rectangle(0, 0, width, height,
    30                                                  fill = fillColor)
    31          self.label = self.canvas.create_text(self.canvas.winfo_reqwidth()
    32  / 2,
    33                                             height / 2, text = labelText,
    34                                             anchor = 'c', fill = labelColor,
    35                                             font = self.labelFont)
    36          self.update()
    37          self.canvas.pack(side = 'top', fill = 'x', expand = 'no')
    38
    39 -    def updateProgress(self, newValue, newMax = None):
    40          if newMax:
    41              self.max = newMax
    42          self.value = newValue
    43          self.update()
    44
    45 -    def update(self):
    46          # Trim the values to be between min and max
    47          value = self.value
    48          if value > self.max:
    49              value = self.max
    50          if value < self.min:
    51              value = self.min
    52          # Adjust the rectangle
    53          if self.orientation == 'horizontal':
    54              self.canvas.coords(self.scale, 0, 0,
    55                float(value) / self.max * self.width, self.height)
    56          else:
    57              self.canvas.coords(self.scale, 0,
    58                                 self.height - (float(value) /
    59                                                self.max * self.height),
    60                                 self.width, self.height)
    61          # Now update the colors
    62          self.canvas.itemconfig(self.scale, fill = self.fillColor)
    63          self.canvas.itemconfig(self.label, fill = self.labelColor)
    64          # And update the label
    65          if self.doLabel:
    66              if value:
    67                  if value >= 0:
    68                      pvalue = int((float(value) / float(self.max)) *
    69                                     100.0)
    70                  else:
    71                      pvalue = 0
    72                  self.canvas.itemconfig(self.label, text = self.labelFormat
    73                                           % pvalue)
    74              else:
    75                  self.canvas.itemconfig(self.label, text = '')
    76          else:
    77              self.canvas.itemconfig(self.label, text = self.labelFormat %
    78                                     self.labelText)
    79          self.canvas.update_idletasks()
    80