ProgressBar.py 3.1 KB

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