PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/Public/Tutorial/winforms.py

#
Python | 76 lines | 33 code | 8 blank | 35 comment | 4 complexity | 725e16fb9b0345de084465398bda1ee4 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. # copy of the license can be found in the License.html file at the root of this distribution. If
  7. # you cannot locate the Apache License, Version 2.0, please send an email to
  8. # ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. # by the terms of the Apache License, Version 2.0.
  10. #
  11. # You must not remove this notice, or any other, from this software.
  12. #
  13. #
  14. #####################################################################################
  15. import clr
  16. clr.AddReferenceByPartialName("System.Windows.Forms")
  17. clr.AddReferenceByPartialName("System.Drawing")
  18. clr.AddReferenceByPartialName("IronPython")
  19. clr.AddReferenceByPartialName("Microsoft.Scripting")
  20. from System.Drawing import Size
  21. from System.Windows.Forms import Form, Application
  22. from System.Threading import Thread
  23. from System.Threading import ThreadStart
  24. from System.Threading import AutoResetEvent
  25. import IronPython
  26. # We support interactive development of Windows Forms by creating another
  27. # thread on which we execute all of the IronPython console's input. This
  28. # allows the form to execute on another thread where a message pump can
  29. # independently handle input and allow the form to repaint. To make this work
  30. # we do two things. First we create a dummy hidden form on the other thread
  31. # which gives us a handle for marshalling the execution of console input onto
  32. # the thread where we will develop our real form. Second, we supply an
  33. # alternative function for the IronPython console to run for executing console
  34. # input. Our alternative function simply invokes the input on the other
  35. # thread.
  36. are = AutoResetEvent(False)
  37. def thread_proc():
  38. try:
  39. global dispatcher
  40. global are
  41. # Create the dummy control, and show then hide it to get Windows Forms
  42. # to initialize it.
  43. dispatcher = Form(Size = Size(0,0))
  44. dispatcher.Show()
  45. dispatcher.Hide()
  46. # Signal that the thread running thread_proc is ready for the main
  47. # thread to send input to it.
  48. are.Set()
  49. # Start the message loop.
  50. Application.Run()
  51. finally:
  52. # In case thread_proc's thread dies, restore the default IronPython
  53. # console execution behavior.
  54. clr.SetCommandDispatcher(None)
  55. def DispatchConsoleCommand(consoleCommand):
  56. if consoleCommand:
  57. # consoleCommand is a delegate for a dynamic method that embodies the
  58. # input expression from the user. Run it on the other thread.
  59. dispatcher.Invoke(consoleCommand)
  60. else:
  61. Application.Exit()
  62. t = Thread(ThreadStart(thread_proc))
  63. t.IsBackground = True
  64. t.Start()
  65. # Don't establish the alternative input execution behavior until the other
  66. # thread is ready. Note, 'are' starts out unsignalled.
  67. are.WaitOne()
  68. clr.SetCommandDispatcher(DispatchConsoleCommand)