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