tk.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Code borrowed from ptpython
  2. # https://github.com/jonathanslenders/ptpython/blob/86b71a89626114b18898a0af463978bdb32eeb70/ptpython/eventloop.py
  3. # Copyright (c) 2015, Jonathan Slenders
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without modification,
  7. # are permitted provided that the following conditions are met:
  8. #
  9. # * Redistributions of source code must retain the above copyright notice, this
  10. # list of conditions and the following disclaimer.
  11. #
  12. # * Redistributions in binary form must reproduce the above copyright notice, this
  13. # list of conditions and the following disclaimer in the documentation and/or
  14. # other materials provided with the distribution.
  15. #
  16. # * Neither the name of the {organization} nor the names of its
  17. # contributors may be used to endorse or promote products derived from
  18. # this software without specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  21. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  24. # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  27. # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. """
  31. Wrapper around the eventloop that gives some time to the Tkinter GUI to process
  32. events when it's loaded and while we are waiting for input at the REPL. This
  33. way we don't block the UI of for instance ``turtle`` and other Tk libraries.
  34. (Normally Tkinter registers it's callbacks in ``PyOS_InputHook`` to integrate
  35. in readline. ``prompt-toolkit`` doesn't understand that input hook, but this
  36. will fix it for Tk.)
  37. """
  38. import time
  39. import _tkinter
  40. import tkinter
  41. def inputhook(inputhook_context):
  42. """
  43. Inputhook for Tk.
  44. Run the Tk eventloop until prompt-toolkit needs to process the next input.
  45. """
  46. # Get the current TK application.
  47. root = tkinter._default_root
  48. def wait_using_filehandler():
  49. """
  50. Run the TK eventloop until the file handler that we got from the
  51. inputhook becomes readable.
  52. """
  53. # Add a handler that sets the stop flag when `prompt-toolkit` has input
  54. # to process.
  55. stop = [False]
  56. def done(*a):
  57. stop[0] = True
  58. root.createfilehandler(inputhook_context.fileno(), _tkinter.READABLE, done)
  59. # Run the TK event loop as long as we don't receive input.
  60. while root.dooneevent(_tkinter.ALL_EVENTS):
  61. if stop[0]:
  62. break
  63. root.deletefilehandler(inputhook_context.fileno())
  64. def wait_using_polling():
  65. """
  66. Windows TK doesn't support 'createfilehandler'.
  67. So, run the TK eventloop and poll until input is ready.
  68. """
  69. while not inputhook_context.input_is_ready():
  70. while root.dooneevent(_tkinter.ALL_EVENTS | _tkinter.DONT_WAIT):
  71. pass
  72. # Sleep to make the CPU idle, but not too long, so that the UI
  73. # stays responsive.
  74. time.sleep(.01)
  75. if root is not None:
  76. if hasattr(root, 'createfilehandler'):
  77. wait_using_filehandler()
  78. else:
  79. wait_using_polling()