Show More
@@ -1,95 +1,90 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 | ||
|
45 | 43 | import _tkinter |
|
46 | if PY3: | |
|
47 | import tkinter | |
|
48 | else: | |
|
49 | import Tkinter as tkinter # Python 2 | |
|
44 | import tkinter | |
|
50 | 45 | |
|
51 | 46 | def inputhook(inputhook_context): |
|
52 | 47 | """ |
|
53 | 48 | Inputhook for Tk. |
|
54 | 49 | Run the Tk eventloop until prompt-toolkit needs to process the next input. |
|
55 | 50 | """ |
|
56 | 51 | # Get the current TK application. |
|
57 | 52 | root = tkinter._default_root |
|
58 | 53 | |
|
59 | 54 | def wait_using_filehandler(): |
|
60 | 55 | """ |
|
61 | 56 | Run the TK eventloop until the file handler that we got from the |
|
62 | 57 | inputhook becomes readable. |
|
63 | 58 | """ |
|
64 | 59 | # Add a handler that sets the stop flag when `prompt-toolkit` has input |
|
65 | 60 | # to process. |
|
66 | 61 | stop = [False] |
|
67 | 62 | def done(*a): |
|
68 | 63 | stop[0] = True |
|
69 | 64 | |
|
70 | 65 | root.createfilehandler(inputhook_context.fileno(), _tkinter.READABLE, done) |
|
71 | 66 | |
|
72 | 67 | # Run the TK event loop as long as we don't receive input. |
|
73 | 68 | while root.dooneevent(_tkinter.ALL_EVENTS): |
|
74 | 69 | if stop[0]: |
|
75 | 70 | break |
|
76 | 71 | |
|
77 | 72 | root.deletefilehandler(inputhook_context.fileno()) |
|
78 | 73 | |
|
79 | 74 | def wait_using_polling(): |
|
80 | 75 | """ |
|
81 | 76 | Windows TK doesn't support 'createfilehandler'. |
|
82 | 77 | So, run the TK eventloop and poll until input is ready. |
|
83 | 78 | """ |
|
84 | 79 | while not inputhook_context.input_is_ready(): |
|
85 | 80 | while root.dooneevent(_tkinter.ALL_EVENTS | _tkinter.DONT_WAIT): |
|
86 | 81 | pass |
|
87 | 82 | # Sleep to make the CPU idle, but not too long, so that the UI |
|
88 | 83 | # stays responsive. |
|
89 | 84 | time.sleep(.01) |
|
90 | 85 | |
|
91 | 86 | if root is not None: |
|
92 | 87 | if hasattr(root, 'createfilehandler'): |
|
93 | 88 | wait_using_filehandler() |
|
94 | 89 | else: |
|
95 | 90 | wait_using_polling() |
General Comments 0
You need to be logged in to leave comments.
Login now