dlgtest.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # A Demo of Pythonwin's Dialog and Property Page support.
  2. ###################
  3. #
  4. # First demo - use the built-in to Pythonwin "Tab Stop" dialog, but
  5. # customise it heavily.
  6. #
  7. # ID's for the tabstop dialog - out test.
  8. #
  9. from win32ui import IDD_SET_TABSTOPS
  10. from win32ui import IDC_EDIT_TABS
  11. from win32ui import IDC_PROMPT_TABS
  12. from win32con import IDOK
  13. from win32con import IDCANCEL
  14. import win32ui
  15. import win32con
  16. from pywin.mfc import dialog
  17. class TestDialog(dialog.Dialog):
  18. def __init__(self, modal=1):
  19. dialog.Dialog.__init__(self, IDD_SET_TABSTOPS)
  20. self.counter=0
  21. if modal:
  22. self.DoModal()
  23. else:
  24. self.CreateWindow()
  25. def OnInitDialog(self):
  26. # Set the caption of the dialog itself.
  27. self.SetWindowText("Used to be Tab Stops!")
  28. # Get a child control, remember it, and change its text.
  29. self.edit=self.GetDlgItem(IDC_EDIT_TABS) # the text box.
  30. self.edit.SetWindowText("Test")
  31. # Hook a Windows message for the dialog.
  32. self.edit.HookMessage(self.KillFocus, win32con.WM_KILLFOCUS)
  33. # Get the prompt control, and change its next.
  34. prompt=self.GetDlgItem(IDC_PROMPT_TABS) # the prompt box.
  35. prompt.SetWindowText("Prompt")
  36. # And the same for the button..
  37. cancel=self.GetDlgItem(IDCANCEL) # the cancel button
  38. cancel.SetWindowText("&Kill me")
  39. # And just for demonstration purposes, we hook the notify message for the dialog.
  40. # This allows us to be notified when the Edit Control text changes.
  41. self.HookCommand(self.OnNotify, IDC_EDIT_TABS)
  42. def OnNotify(self, controlid, code):
  43. if code==win32con.EN_CHANGE:
  44. print("Edit text changed!")
  45. return 1 # I handled this, so no need to call defaults!
  46. # kill focus for the edit box.
  47. # Simply increment the value in the text box.
  48. def KillFocus(self,msg):
  49. self.counter=self.counter+1
  50. if self.edit != None:
  51. self.edit.SetWindowText(str(self.counter))
  52. # Called when the dialog box is terminating...
  53. def OnDestroy(self,msg):
  54. del self.edit
  55. del self.counter
  56. # A very simply Property Sheet.
  57. # We only make a new class for demonstration purposes.
  58. class TestSheet(dialog.PropertySheet):
  59. def __init__(self, title):
  60. dialog.PropertySheet.__init__(self, title)
  61. self.HookMessage(self.OnActivate, win32con.WM_ACTIVATE)
  62. def OnActivate(self, msg):
  63. pass
  64. # A very simply Property Page, which will be "owned" by the above
  65. # Property Sheet.
  66. # We create a new class, just so we can hook a control notification.
  67. class TestPage(dialog.PropertyPage):
  68. def OnInitDialog(self):
  69. # We use the HookNotify function to allow Python to respond to
  70. # Windows WM_NOTIFY messages.
  71. # In this case, we are interested in BN_CLICKED messages.
  72. self.HookNotify(self.OnNotify, win32con.BN_CLICKED)
  73. def OnNotify(self, std, extra):
  74. print("OnNotify", std, extra)
  75. # Some code that actually uses these objects.
  76. def demo(modal = 0):
  77. TestDialog(modal)
  78. # property sheet/page demo
  79. ps=win32ui.CreatePropertySheet('Property Sheet/Page Demo')
  80. # Create a completely standard PropertyPage.
  81. page1=win32ui.CreatePropertyPage(win32ui.IDD_PROPDEMO1)
  82. # Create our custom property page.
  83. page2=TestPage(win32ui.IDD_PROPDEMO2)
  84. ps.AddPage(page1)
  85. ps.AddPage(page2)
  86. if modal:
  87. ps.DoModal()
  88. else:
  89. style = win32con.WS_SYSMENU|win32con.WS_POPUP|win32con.WS_CAPTION|win32con.DS_MODALFRAME|win32con.WS_VISIBLE
  90. styleex = win32con.WS_EX_DLGMODALFRAME | win32con.WS_EX_PALETTEWINDOW
  91. ps.CreateWindow(win32ui.GetMainFrame(), style, styleex)
  92. def test(modal=1):
  93. # dlg=dialog.Dialog(1010)
  94. # dlg.CreateWindow()
  95. # dlg.EndDialog(0)
  96. # del dlg
  97. # return
  98. # property sheet/page demo
  99. ps=TestSheet('Property Sheet/Page Demo')
  100. page1=win32ui.CreatePropertyPage(win32ui.IDD_PROPDEMO1)
  101. page2=win32ui.CreatePropertyPage(win32ui.IDD_PROPDEMO2)
  102. ps.AddPage(page1)
  103. ps.AddPage(page2)
  104. del page1
  105. del page2
  106. if modal:
  107. ps.DoModal()
  108. else:
  109. ps.CreateWindow(win32ui.GetMainFrame())
  110. return ps
  111. def d():
  112. dlg = win32ui.CreateDialog(win32ui.IDD_DEBUGGER)
  113. dlg.datalist.append((win32ui.IDC_DBG_RADIOSTACK, "radio"))
  114. print("data list is ", dlg.datalist)
  115. dlg.data['radio']=1
  116. dlg.DoModal()
  117. print(dlg.data['radio'])
  118. if __name__=='__main__':
  119. demo(1)