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