progressbar.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #
  2. # Progress bar control example
  3. #
  4. # PyCProgressCtrl encapsulates the MFC CProgressCtrl class. To use it,
  5. # you:
  6. #
  7. # - Create the control with win32ui.CreateProgressCtrl()
  8. # - Create the control window with PyCProgressCtrl.CreateWindow()
  9. # - Initialize the range if you want it to be other than (0, 100) using
  10. # PyCProgressCtrl.SetRange()
  11. # - Either:
  12. # - Set the step size with PyCProgressCtrl.SetStep(), and
  13. # - Increment using PyCProgressCtrl.StepIt()
  14. # or:
  15. # - Set the amount completed using PyCProgressCtrl.SetPos()
  16. #
  17. # Example and progress bar code courtesy of KDL Technologies, Ltd., Hong Kong SAR, China.
  18. #
  19. from pywin.mfc import dialog
  20. import win32ui
  21. import win32con
  22. def MakeDlgTemplate():
  23. style = (win32con.DS_MODALFRAME |
  24. win32con.WS_POPUP |
  25. win32con.WS_VISIBLE |
  26. win32con.WS_CAPTION |
  27. win32con.WS_SYSMENU |
  28. win32con.DS_SETFONT)
  29. cs = (win32con.WS_CHILD |
  30. win32con.WS_VISIBLE)
  31. w = 215
  32. h = 36
  33. dlg = [["Progress bar control example",
  34. (0, 0, w, h),
  35. style,
  36. None,
  37. (8, "MS Sans Serif")],
  38. ]
  39. s = win32con.WS_TABSTOP | cs
  40. dlg.append([128,
  41. "Tick",
  42. win32con.IDOK,
  43. (10, h - 18, 50, 14), s | win32con.BS_DEFPUSHBUTTON])
  44. dlg.append([128,
  45. "Cancel",
  46. win32con.IDCANCEL,
  47. (w - 60, h - 18, 50, 14), s | win32con.BS_PUSHBUTTON])
  48. return dlg
  49. class TestDialog(dialog.Dialog):
  50. def OnInitDialog(self):
  51. rc = dialog.Dialog.OnInitDialog(self)
  52. self.pbar = win32ui.CreateProgressCtrl()
  53. self.pbar.CreateWindow (win32con.WS_CHILD |
  54. win32con.WS_VISIBLE,
  55. (10, 10, 310, 24),
  56. self, 1001)
  57. # self.pbar.SetStep (5)
  58. self.progress = 0
  59. self.pincr = 5
  60. return rc
  61. def OnOK(self):
  62. # NB: StepIt wraps at the end if you increment past the upper limit!
  63. # self.pbar.StepIt()
  64. self.progress = self.progress + self.pincr
  65. if self.progress > 100:
  66. self.progress = 100
  67. if self.progress <= 100:
  68. self.pbar.SetPos(self.progress)
  69. def demo(modal = 0):
  70. d = TestDialog (MakeDlgTemplate())
  71. if modal:
  72. d.DoModal()
  73. else:
  74. d.CreateWindow ()
  75. if __name__=='__main__':
  76. demo(1)