dlgappdemo.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # dlgappdemo - a demo of a dialog application.
  2. # This is a demonstration of both a custom "application" module,
  3. # and a Python program in a dialog box.
  4. #
  5. # NOTE: You CAN NOT import this module from either PythonWin or Python.
  6. # This module must be specified on the commandline to PythonWin only.
  7. # eg, PythonWin /app dlgappdemo.py
  8. from pywin.framework import dlgappcore, app
  9. import win32ui
  10. import sys
  11. class TestDialogApp(dlgappcore.DialogApp):
  12. def CreateDialog(self):
  13. return TestAppDialog()
  14. class TestAppDialog(dlgappcore.AppDialog):
  15. def __init__(self):
  16. self.edit = None
  17. dlgappcore.AppDialog.__init__(self, win32ui.IDD_LARGE_EDIT)
  18. def OnInitDialog(self):
  19. self.SetWindowText('Test dialog application')
  20. self.edit = self.GetDlgItem(win32ui.IDC_EDIT1)
  21. print("Hello from Python")
  22. print("args are:", end=' ')
  23. for arg in sys.argv:
  24. print(arg)
  25. return 1
  26. def PreDoModal(self):
  27. sys.stdout = sys.stderr = self
  28. def write(self, str):
  29. if self.edit:
  30. self.edit.SetSel(-2)
  31. # translate \n to \n\r
  32. self.edit.ReplaceSel(str.replace('\n','\r\n'))
  33. else:
  34. win32ui.OutputDebug("dlgapp - no edit control! >>\n%s\n<<\n" % str )
  35. app.AppBuilder = TestDialogApp
  36. if __name__=='__main__':
  37. import demoutils
  38. demoutils.NeedApp()