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