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