A simple progress bar that shows a percentage progress in the given colour.
| 431 | |
| 432 | |
| 433 | class ProgressBar(tk.Frame): |
| 434 | """A simple progress bar that shows a percentage progress in |
| 435 | the given colour.""" |
| 436 | |
| 437 | def __init__(self, *args, **kwargs): |
| 438 | tk.Frame.__init__(self, *args, **kwargs) |
| 439 | self.canvas = tk.Canvas(self, height='20', width='60', |
| 440 | background='white', borderwidth=3) |
| 441 | self.canvas.pack(fill=tk.X, expand=1) |
| 442 | self.rect = self.text = None |
| 443 | self.canvas.bind('<Configure>', self.paint) |
| 444 | self.setProgressFraction(0.0) |
| 445 | |
| 446 | def setProgressFraction(self, fraction, color='blue'): |
| 447 | self.fraction = fraction |
| 448 | self.color = color |
| 449 | self.paint() |
| 450 | self.canvas.update_idletasks() |
| 451 | |
| 452 | def paint(self, *args): |
| 453 | totalWidth = self.canvas.winfo_width() |
| 454 | width = int(self.fraction * float(totalWidth)) |
| 455 | height = self.canvas.winfo_height() |
| 456 | if self.rect is not None: self.canvas.delete(self.rect) |
| 457 | if self.text is not None: self.canvas.delete(self.text) |
| 458 | self.rect = self.canvas.create_rectangle(0, 0, width, height, |
| 459 | fill=self.color) |
| 460 | percentString = "%3.0f%%" % (100.0 * self.fraction) |
| 461 | self.text = self.canvas.create_text(totalWidth/2, height/2, |
| 462 | anchor=tk.CENTER, |
| 463 | text=percentString) |
| 464 | |
| 465 | def main(initialTestName=""): |
| 466 | root = tk.Tk() |
no outgoing calls
searching dependent graphs…