Show More
@@ -0,0 +1,103 b'' | |||
|
1 | ================================ | |
|
2 | Integrating with GUI event loops | |
|
3 | ================================ | |
|
4 | ||
|
5 | When the user types ``%gui qt``, IPython integrates itself with the Qt event | |
|
6 | loop, so you can use both a GUI and an interactive prompt together. IPython | |
|
7 | supports a number of common GUI toolkits, but from IPython 3.0, it is possible | |
|
8 | to integrate other event loops without modifying IPython itself. | |
|
9 | ||
|
10 | Terminal IPython handles event loops very differently from the IPython kernel, | |
|
11 | so different steps are needed to integrate with each. | |
|
12 | ||
|
13 | Event loops in the terminal | |
|
14 | --------------------------- | |
|
15 | ||
|
16 | In the terminal, IPython uses a blocking Python function to wait for user input. | |
|
17 | However, the Python C API provides a hook, :c:func:`PyOS_InputHook`, which is | |
|
18 | called frequently while waiting for input. This can be set to a function which | |
|
19 | briefly runs the event loop and then returns. | |
|
20 | ||
|
21 | IPython provides Python level wrappers for setting and resetting this hook. To | |
|
22 | use them, subclass :class:`IPython.lib.inputhook.InputHookBase`, and define | |
|
23 | an ``enable(app=None)`` method, which initialises the event loop and calls | |
|
24 | ``self.manager.set_inputhook(f)`` with a function which will briefly run the | |
|
25 | event loop before exiting. Decorate the class with a call to | |
|
26 | :func:`IPython.lib.inputhook.register`:: | |
|
27 | ||
|
28 | from IPython.lib.inputhook import register, InputHookBase | |
|
29 | ||
|
30 | @register('clutter') | |
|
31 | class ClutterInputHook(InputHookBase): | |
|
32 | def enable(self, app=None): | |
|
33 | self.manager.set_inputhook(inputhook_clutter) | |
|
34 | ||
|
35 | You can also optionally define a ``disable()`` method, taking no arguments, if | |
|
36 | there are extra steps needed to clean up. IPython will take care of resetting | |
|
37 | the hook, whether or not you provide a disable method. | |
|
38 | ||
|
39 | The simplest way to define the hook function is just to run one iteration of the | |
|
40 | event loop, or to run until no events are pending. Most event loops provide some | |
|
41 | mechanism to do one of these things. However, the GUI may lag slightly, | |
|
42 | because the hook is only called every 0.1 seconds. Alternatively, the hook can | |
|
43 | keep running the event loop until there is input ready on stdin. IPython | |
|
44 | provides a function to facilitate this: | |
|
45 | ||
|
46 | .. currentmodule:: IPython.lib.inputhook | |
|
47 | ||
|
48 | .. function:: stdin_ready() | |
|
49 | ||
|
50 | Returns True if there is something ready to read on stdin. | |
|
51 | ||
|
52 | If this is the case, the hook function should return immediately. | |
|
53 | ||
|
54 | This is implemented for Windows and POSIX systems - on other platforms, it | |
|
55 | always returns True, so that the hook always gives Python a chance to check | |
|
56 | for input. | |
|
57 | ||
|
58 | ||
|
59 | Event loops in the kernel | |
|
60 | ------------------------- | |
|
61 | ||
|
62 | The kernel runs its own event loop, so it's simpler to integrate with others. | |
|
63 | IPython allows the other event loop to take control, but it must call | |
|
64 | :meth:`IPython.kernel.zmq.kernelbase.Kernel.do_one_iteration` periodically. | |
|
65 | ||
|
66 | To integrate with this, write a function that takes a single argument, | |
|
67 | the IPython kernel instance, arranges for your event loop to call | |
|
68 | ``kernel.do_one_iteration()`` at least every ``kernel._poll_interval`` seconds, | |
|
69 | and starts the event loop. | |
|
70 | ||
|
71 | Decorate this function with :func:`IPython.kernel.zmq.eventloops.register_integration`, | |
|
72 | passing in the names you wish to register it for. Here is a slightly simplified | |
|
73 | version of the Tkinter integration already included in IPython:: | |
|
74 | ||
|
75 | @register_integration('tk') | |
|
76 | def loop_tk(kernel): | |
|
77 | """Start a kernel with the Tk event loop.""" | |
|
78 | from tkinter import Tk | |
|
79 | ||
|
80 | # Tk uses milliseconds | |
|
81 | poll_interval = int(1000*kernel._poll_interval) | |
|
82 | # For Tkinter, we create a Tk object and call its withdraw method. | |
|
83 | class Timer(object): | |
|
84 | def __init__(self, func): | |
|
85 | self.app = Tk() | |
|
86 | self.app.withdraw() | |
|
87 | self.func = func | |
|
88 | ||
|
89 | def on_timer(self): | |
|
90 | self.func() | |
|
91 | self.app.after(poll_interval, self.on_timer) | |
|
92 | ||
|
93 | def start(self): | |
|
94 | self.on_timer() # Call it once to get things going. | |
|
95 | self.app.mainloop() | |
|
96 | ||
|
97 | kernel.timer = Timer(kernel.do_one_iteration) | |
|
98 | kernel.timer.start() | |
|
99 | ||
|
100 | Some event loops can go one better, and integrate checking for messages on the | |
|
101 | kernel's ZMQ sockets, making the kernel more responsive than plain polling. How | |
|
102 | to do this is outside the scope of this document; if you are interested, look at | |
|
103 | the integration with Qt in :mod:`IPython.kernel.zmq.eventloops`. |
@@ -0,0 +1,4 b'' | |||
|
1 | * It's now possible to provide mechanisms to integrate IPython with other event | |
|
2 | loops, in addition to the ones we already support. This lets you run GUI code | |
|
3 | in IPython with an interactive prompt, and to embed the IPython | |
|
4 | kernel in GUI applications. See :doc:`/config/eventloops` for details. |
@@ -1,561 +1,560 b'' | |||
|
1 | 1 | # coding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | Inputhook management for GUI event loop integration. |
|
4 | 4 | """ |
|
5 | 5 | |
|
6 | 6 | #----------------------------------------------------------------------------- |
|
7 | 7 | # Copyright (C) 2008-2011 The IPython Development Team |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | 17 | try: |
|
18 | 18 | import ctypes |
|
19 | 19 | except ImportError: |
|
20 | 20 | ctypes = None |
|
21 | 21 | except SystemError: # IronPython issue, 2/8/2014 |
|
22 | 22 | ctypes = None |
|
23 | 23 | import os |
|
24 | 24 | import sys |
|
25 | 25 | from distutils.version import LooseVersion as V |
|
26 | 26 | |
|
27 | 27 | from IPython.utils.warn import warn |
|
28 | 28 | |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | # Constants |
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | |
|
33 | 33 | # Constants for identifying the GUI toolkits. |
|
34 | 34 | GUI_WX = 'wx' |
|
35 | 35 | GUI_QT = 'qt' |
|
36 | 36 | GUI_QT4 = 'qt4' |
|
37 | 37 | GUI_GTK = 'gtk' |
|
38 | 38 | GUI_TK = 'tk' |
|
39 | 39 | GUI_OSX = 'osx' |
|
40 | 40 | GUI_GLUT = 'glut' |
|
41 | 41 | GUI_PYGLET = 'pyglet' |
|
42 | 42 | GUI_GTK3 = 'gtk3' |
|
43 | 43 | GUI_NONE = 'none' # i.e. disable |
|
44 | 44 | |
|
45 | 45 | #----------------------------------------------------------------------------- |
|
46 | 46 | # Utilities |
|
47 | 47 | #----------------------------------------------------------------------------- |
|
48 | 48 | |
|
49 | 49 | def _stdin_ready_posix(): |
|
50 | 50 | """Return True if there's something to read on stdin (posix version).""" |
|
51 | 51 | infds, outfds, erfds = select.select([sys.stdin],[],[],0) |
|
52 | 52 | return bool(infds) |
|
53 | 53 | |
|
54 | 54 | def _stdin_ready_nt(): |
|
55 | 55 | """Return True if there's something to read on stdin (nt version).""" |
|
56 | 56 | return msvcrt.kbhit() |
|
57 | 57 | |
|
58 | 58 | def _stdin_ready_other(): |
|
59 | 59 | """Return True, assuming there's something to read on stdin.""" |
|
60 | 60 | return True # |
|
61 | 61 | |
|
62 | 62 | |
|
63 | 63 | def _ignore_CTRL_C_posix(): |
|
64 | 64 | """Ignore CTRL+C (SIGINT).""" |
|
65 | 65 | signal.signal(signal.SIGINT, signal.SIG_IGN) |
|
66 | 66 | |
|
67 | 67 | def _allow_CTRL_C_posix(): |
|
68 | 68 | """Take CTRL+C into account (SIGINT).""" |
|
69 | 69 | signal.signal(signal.SIGINT, signal.default_int_handler) |
|
70 | 70 | |
|
71 | 71 | def _ignore_CTRL_C_other(): |
|
72 | 72 | """Ignore CTRL+C (not implemented).""" |
|
73 | 73 | pass |
|
74 | 74 | |
|
75 | 75 | def _allow_CTRL_C_other(): |
|
76 | 76 | """Take CTRL+C into account (not implemented).""" |
|
77 | 77 | pass |
|
78 | 78 | |
|
79 | 79 | if os.name == 'posix': |
|
80 | 80 | import select |
|
81 | 81 | import signal |
|
82 | 82 | stdin_ready = _stdin_ready_posix |
|
83 | 83 | ignore_CTRL_C = _ignore_CTRL_C_posix |
|
84 | 84 | allow_CTRL_C = _allow_CTRL_C_posix |
|
85 | 85 | elif os.name == 'nt': |
|
86 | 86 | import msvcrt |
|
87 | 87 | stdin_ready = _stdin_ready_nt |
|
88 | 88 | ignore_CTRL_C = _ignore_CTRL_C_other |
|
89 | 89 | allow_CTRL_C = _allow_CTRL_C_other |
|
90 | 90 | else: |
|
91 | 91 | stdin_ready = _stdin_ready_other |
|
92 | 92 | ignore_CTRL_C = _ignore_CTRL_C_other |
|
93 | 93 | allow_CTRL_C = _allow_CTRL_C_other |
|
94 | 94 | |
|
95 | 95 | |
|
96 | 96 | #----------------------------------------------------------------------------- |
|
97 | 97 | # Main InputHookManager class |
|
98 | 98 | #----------------------------------------------------------------------------- |
|
99 | 99 | |
|
100 | 100 | |
|
101 | 101 | class InputHookManager(object): |
|
102 | 102 | """Manage PyOS_InputHook for different GUI toolkits. |
|
103 | 103 | |
|
104 | 104 | This class installs various hooks under ``PyOSInputHook`` to handle |
|
105 | 105 | GUI event loop integration. |
|
106 | 106 | """ |
|
107 | 107 | |
|
108 | 108 | def __init__(self): |
|
109 | 109 | if ctypes is None: |
|
110 | 110 | warn("IPython GUI event loop requires ctypes, %gui will not be available") |
|
111 | 111 | return |
|
112 | 112 | self.PYFUNC = ctypes.PYFUNCTYPE(ctypes.c_int) |
|
113 | 113 | self.guihooks = {} |
|
114 | 114 | self.aliases = {} |
|
115 | 115 | self.apps = {} |
|
116 | 116 | self._reset() |
|
117 | 117 | |
|
118 | 118 | def _reset(self): |
|
119 | 119 | self._callback_pyfunctype = None |
|
120 | 120 | self._callback = None |
|
121 | 121 | self._installed = False |
|
122 | 122 | self._current_gui = None |
|
123 | 123 | |
|
124 | 124 | def get_pyos_inputhook(self): |
|
125 | 125 | """Return the current PyOS_InputHook as a ctypes.c_void_p.""" |
|
126 | 126 | return ctypes.c_void_p.in_dll(ctypes.pythonapi,"PyOS_InputHook") |
|
127 | 127 | |
|
128 | 128 | def get_pyos_inputhook_as_func(self): |
|
129 | 129 | """Return the current PyOS_InputHook as a ctypes.PYFUNCYPE.""" |
|
130 | 130 | return self.PYFUNC.in_dll(ctypes.pythonapi,"PyOS_InputHook") |
|
131 | 131 | |
|
132 | 132 | def set_inputhook(self, callback): |
|
133 | 133 | """Set PyOS_InputHook to callback and return the previous one.""" |
|
134 | 134 | # On platforms with 'readline' support, it's all too likely to |
|
135 | 135 | # have a KeyboardInterrupt signal delivered *even before* an |
|
136 | 136 | # initial ``try:`` clause in the callback can be executed, so |
|
137 | 137 | # we need to disable CTRL+C in this situation. |
|
138 | 138 | ignore_CTRL_C() |
|
139 | 139 | self._callback = callback |
|
140 | 140 | self._callback_pyfunctype = self.PYFUNC(callback) |
|
141 | 141 | pyos_inputhook_ptr = self.get_pyos_inputhook() |
|
142 | 142 | original = self.get_pyos_inputhook_as_func() |
|
143 | 143 | pyos_inputhook_ptr.value = \ |
|
144 | 144 | ctypes.cast(self._callback_pyfunctype, ctypes.c_void_p).value |
|
145 | 145 | self._installed = True |
|
146 | 146 | return original |
|
147 | 147 | |
|
148 | 148 | def clear_inputhook(self, app=None): |
|
149 | 149 | """Set PyOS_InputHook to NULL and return the previous one. |
|
150 | 150 | |
|
151 | 151 | Parameters |
|
152 | 152 | ---------- |
|
153 | 153 | app : optional, ignored |
|
154 | 154 | This parameter is allowed only so that clear_inputhook() can be |
|
155 | 155 | called with a similar interface as all the ``enable_*`` methods. But |
|
156 | 156 | the actual value of the parameter is ignored. This uniform interface |
|
157 | 157 | makes it easier to have user-level entry points in the main IPython |
|
158 | 158 | app like :meth:`enable_gui`.""" |
|
159 | 159 | pyos_inputhook_ptr = self.get_pyos_inputhook() |
|
160 | 160 | original = self.get_pyos_inputhook_as_func() |
|
161 | 161 | pyos_inputhook_ptr.value = ctypes.c_void_p(None).value |
|
162 | 162 | allow_CTRL_C() |
|
163 | 163 | self._reset() |
|
164 | 164 | return original |
|
165 | 165 | |
|
166 | 166 | def clear_app_refs(self, gui=None): |
|
167 | 167 | """Clear IPython's internal reference to an application instance. |
|
168 | 168 | |
|
169 | 169 | Whenever we create an app for a user on qt4 or wx, we hold a |
|
170 | 170 | reference to the app. This is needed because in some cases bad things |
|
171 | 171 | can happen if a user doesn't hold a reference themselves. This |
|
172 | 172 | method is provided to clear the references we are holding. |
|
173 | 173 | |
|
174 | 174 | Parameters |
|
175 | 175 | ---------- |
|
176 | 176 | gui : None or str |
|
177 | 177 | If None, clear all app references. If ('wx', 'qt4') clear |
|
178 | 178 | the app for that toolkit. References are not held for gtk or tk |
|
179 | 179 | as those toolkits don't have the notion of an app. |
|
180 | 180 | """ |
|
181 | 181 | if gui is None: |
|
182 | 182 | self.apps = {} |
|
183 | 183 | elif gui in self.apps: |
|
184 | 184 | del self.apps[gui] |
|
185 | 185 | |
|
186 | 186 | def register(self, toolkitname, *aliases): |
|
187 | 187 | """Register a class to provide the event loop for a given GUI. |
|
188 | 188 | |
|
189 | 189 | This is intended to be used as a class decorator. It should be passed |
|
190 | 190 | the names with which to register this GUI integration. The classes |
|
191 | 191 | themselves should subclass :class:`InputHookBase`. |
|
192 | 192 | |
|
193 |
|
|
|
194 | -------- | |
|
193 | :: | |
|
195 | 194 | |
|
196 | @inputhook_manager.register('qt') | |
|
197 | class QtInputHook(InputHookBase): | |
|
198 | def enable(self, app=None): | |
|
199 | ... | |
|
195 | @inputhook_manager.register('qt') | |
|
196 | class QtInputHook(InputHookBase): | |
|
197 | def enable(self, app=None): | |
|
198 | ... | |
|
200 | 199 | """ |
|
201 | 200 | def decorator(cls): |
|
202 | 201 | inst = cls(self) |
|
203 | 202 | self.guihooks[toolkitname] = inst |
|
204 | 203 | for a in aliases: |
|
205 | 204 | self.aliases[a] = toolkitname |
|
206 | 205 | return cls |
|
207 | 206 | return decorator |
|
208 | 207 | |
|
209 | 208 | def current_gui(self): |
|
210 | 209 | """Return a string indicating the currently active GUI or None.""" |
|
211 | 210 | return self._current_gui |
|
212 | 211 | |
|
213 | 212 | def enable_gui(self, gui=None, app=None): |
|
214 | 213 | """Switch amongst GUI input hooks by name. |
|
215 | 214 | |
|
216 | 215 | This is a higher level method than :meth:`set_inputhook` - it uses the |
|
217 | 216 | GUI name to look up a registered object which enables the input hook |
|
218 | 217 | for that GUI. |
|
219 | 218 | |
|
220 | 219 | Parameters |
|
221 | 220 | ---------- |
|
222 | 221 | gui : optional, string or None |
|
223 | 222 | If None (or 'none'), clears input hook, otherwise it must be one |
|
224 | 223 | of the recognized GUI names (see ``GUI_*`` constants in module). |
|
225 | 224 | |
|
226 | 225 | app : optional, existing application object. |
|
227 | 226 | For toolkits that have the concept of a global app, you can supply an |
|
228 | 227 | existing one. If not given, the toolkit will be probed for one, and if |
|
229 | 228 | none is found, a new one will be created. Note that GTK does not have |
|
230 | 229 | this concept, and passing an app if ``gui=="GTK"`` will raise an error. |
|
231 | 230 | |
|
232 | 231 | Returns |
|
233 | 232 | ------- |
|
234 | 233 | The output of the underlying gui switch routine, typically the actual |
|
235 | 234 | PyOS_InputHook wrapper object or the GUI toolkit app created, if there was |
|
236 | 235 | one. |
|
237 | 236 | """ |
|
238 | 237 | if gui in (None, GUI_NONE): |
|
239 | 238 | return self.disable_gui() |
|
240 | 239 | |
|
241 | 240 | if gui in self.aliases: |
|
242 | 241 | return self.enable_gui(self.aliases[gui], app) |
|
243 | 242 | |
|
244 | 243 | try: |
|
245 | 244 | gui_hook = self.guihooks[gui] |
|
246 | 245 | except KeyError: |
|
247 | 246 | e = "Invalid GUI request {!r}, valid ones are: {}" |
|
248 | 247 | raise ValueError(e.format(gui, ', '.join(self.guihooks))) |
|
249 | 248 | self._current_gui = gui |
|
250 | 249 | return gui_hook.enable(app) |
|
251 | 250 | |
|
252 | 251 | def disable_gui(self): |
|
253 | 252 | """Disable GUI event loop integration. |
|
254 | 253 | |
|
255 | 254 | If an application was registered, this sets its ``_in_event_loop`` |
|
256 | 255 | attribute to False. It then calls :meth:`clear_inputhook`. |
|
257 | 256 | """ |
|
258 | 257 | gui = self._current_gui |
|
259 | 258 | if gui in self.apps: |
|
260 | 259 | self.apps[gui]._in_event_loop = False |
|
261 | 260 | return self.clear_inputhook() |
|
262 | 261 | |
|
263 | 262 | class InputHookBase(object): |
|
264 | 263 | """Base class for input hooks for specific toolkits. |
|
265 | 264 | |
|
266 | 265 | Subclasses should define an :meth:`enable` method with one argument, ``app``, |
|
267 | 266 | which will either be an instance of the toolkit's application class, or None. |
|
268 | 267 | They may also define a :meth:`disable` method with no arguments. |
|
269 | 268 | """ |
|
270 | 269 | def __init__(self, manager): |
|
271 | 270 | self.manager = manager |
|
272 | 271 | |
|
273 | 272 | def disable(self): |
|
274 | 273 | pass |
|
275 | 274 | |
|
276 | 275 | inputhook_manager = InputHookManager() |
|
277 | 276 | |
|
278 | 277 | @inputhook_manager.register('wx') |
|
279 | 278 | class WxInputHook(InputHookBase): |
|
280 | 279 | def enable(self, app=None): |
|
281 | 280 | """Enable event loop integration with wxPython. |
|
282 | 281 | |
|
283 | 282 | Parameters |
|
284 | 283 | ---------- |
|
285 | 284 | app : WX Application, optional. |
|
286 | 285 | Running application to use. If not given, we probe WX for an |
|
287 | 286 | existing application object, and create a new one if none is found. |
|
288 | 287 | |
|
289 | 288 | Notes |
|
290 | 289 | ----- |
|
291 | 290 | This methods sets the ``PyOS_InputHook`` for wxPython, which allows |
|
292 | 291 | the wxPython to integrate with terminal based applications like |
|
293 | 292 | IPython. |
|
294 | 293 | |
|
295 | 294 | If ``app`` is not given we probe for an existing one, and return it if |
|
296 | 295 | found. If no existing app is found, we create an :class:`wx.App` as |
|
297 | 296 | follows:: |
|
298 | 297 | |
|
299 | 298 | import wx |
|
300 | 299 | app = wx.App(redirect=False, clearSigInt=False) |
|
301 | 300 | """ |
|
302 | 301 | import wx |
|
303 | 302 | |
|
304 | 303 | wx_version = V(wx.__version__).version |
|
305 | 304 | |
|
306 | 305 | if wx_version < [2, 8]: |
|
307 | 306 | raise ValueError("requires wxPython >= 2.8, but you have %s" % wx.__version__) |
|
308 | 307 | |
|
309 | 308 | from IPython.lib.inputhookwx import inputhook_wx |
|
310 | 309 | from IPython.external.appnope import nope |
|
311 | 310 | self.manager.set_inputhook(inputhook_wx) |
|
312 | 311 | nope() |
|
313 | 312 | |
|
314 | 313 | import wx |
|
315 | 314 | if app is None: |
|
316 | 315 | app = wx.GetApp() |
|
317 | 316 | if app is None: |
|
318 | 317 | app = wx.App(redirect=False, clearSigInt=False) |
|
319 | 318 | app._in_event_loop = True |
|
320 | 319 | self.manager.apps[GUI_WX] = app |
|
321 | 320 | return app |
|
322 | 321 | |
|
323 | 322 | def disable(self): |
|
324 | 323 | """Disable event loop integration with wxPython. |
|
325 | 324 | |
|
326 | 325 | This restores appnapp on OS X |
|
327 | 326 | """ |
|
328 | 327 | from IPython.external.appnope import nap |
|
329 | 328 | nap() |
|
330 | 329 | |
|
331 | 330 | @inputhook_manager.register('qt', 'qt4') |
|
332 | 331 | class Qt4InputHook(InputHookBase): |
|
333 | 332 | def enable(self, app=None): |
|
334 | 333 | """Enable event loop integration with PyQt4. |
|
335 | 334 | |
|
336 | 335 | Parameters |
|
337 | 336 | ---------- |
|
338 | 337 | app : Qt Application, optional. |
|
339 | 338 | Running application to use. If not given, we probe Qt for an |
|
340 | 339 | existing application object, and create a new one if none is found. |
|
341 | 340 | |
|
342 | 341 | Notes |
|
343 | 342 | ----- |
|
344 | 343 | This methods sets the PyOS_InputHook for PyQt4, which allows |
|
345 | 344 | the PyQt4 to integrate with terminal based applications like |
|
346 | 345 | IPython. |
|
347 | 346 | |
|
348 | 347 | If ``app`` is not given we probe for an existing one, and return it if |
|
349 | 348 | found. If no existing app is found, we create an :class:`QApplication` |
|
350 | 349 | as follows:: |
|
351 | 350 | |
|
352 | 351 | from PyQt4 import QtCore |
|
353 | 352 | app = QtGui.QApplication(sys.argv) |
|
354 | 353 | """ |
|
355 | 354 | from IPython.lib.inputhookqt4 import create_inputhook_qt4 |
|
356 | 355 | from IPython.external.appnope import nope |
|
357 | 356 | app, inputhook_qt4 = create_inputhook_qt4(self, app) |
|
358 | 357 | self.manager.set_inputhook(inputhook_qt4) |
|
359 | 358 | nope() |
|
360 | 359 | |
|
361 | 360 | app._in_event_loop = True |
|
362 | 361 | self.manager.apps[GUI_QT4] = app |
|
363 | 362 | return app |
|
364 | 363 | |
|
365 | 364 | def disable_qt4(self): |
|
366 | 365 | """Disable event loop integration with PyQt4. |
|
367 | 366 | |
|
368 | 367 | This restores appnapp on OS X |
|
369 | 368 | """ |
|
370 | 369 | from IPython.external.appnope import nap |
|
371 | 370 | nap() |
|
372 | 371 | |
|
373 | 372 | @inputhook_manager.register('gtk') |
|
374 | 373 | class GtkInputHook(InputHookBase): |
|
375 | 374 | def enable(self, app=None): |
|
376 | 375 | """Enable event loop integration with PyGTK. |
|
377 | 376 | |
|
378 | 377 | Parameters |
|
379 | 378 | ---------- |
|
380 | 379 | app : ignored |
|
381 | 380 | Ignored, it's only a placeholder to keep the call signature of all |
|
382 | 381 | gui activation methods consistent, which simplifies the logic of |
|
383 | 382 | supporting magics. |
|
384 | 383 | |
|
385 | 384 | Notes |
|
386 | 385 | ----- |
|
387 | 386 | This methods sets the PyOS_InputHook for PyGTK, which allows |
|
388 | 387 | the PyGTK to integrate with terminal based applications like |
|
389 | 388 | IPython. |
|
390 | 389 | """ |
|
391 | 390 | import gtk |
|
392 | 391 | try: |
|
393 | 392 | gtk.set_interactive(True) |
|
394 | 393 | except AttributeError: |
|
395 | 394 | # For older versions of gtk, use our own ctypes version |
|
396 | 395 | from IPython.lib.inputhookgtk import inputhook_gtk |
|
397 | 396 | self.manager.set_inputhook(inputhook_gtk) |
|
398 | 397 | |
|
399 | 398 | |
|
400 | 399 | @inputhook_manager.register('tk') |
|
401 | 400 | class TkInputHook(InputHookBase): |
|
402 | 401 | def enable(self, app=None): |
|
403 | 402 | """Enable event loop integration with Tk. |
|
404 | 403 | |
|
405 | 404 | Parameters |
|
406 | 405 | ---------- |
|
407 | 406 | app : toplevel :class:`Tkinter.Tk` widget, optional. |
|
408 | 407 | Running toplevel widget to use. If not given, we probe Tk for an |
|
409 | 408 | existing one, and create a new one if none is found. |
|
410 | 409 | |
|
411 | 410 | Notes |
|
412 | 411 | ----- |
|
413 | 412 | If you have already created a :class:`Tkinter.Tk` object, the only |
|
414 | 413 | thing done by this method is to register with the |
|
415 | 414 | :class:`InputHookManager`, since creating that object automatically |
|
416 | 415 | sets ``PyOS_InputHook``. |
|
417 | 416 | """ |
|
418 | 417 | if app is None: |
|
419 | 418 | try: |
|
420 | 419 | from tkinter import Tk # Py 3 |
|
421 | 420 | except ImportError: |
|
422 | 421 | from Tkinter import Tk # Py 2 |
|
423 | 422 | app = Tk() |
|
424 | 423 | app.withdraw() |
|
425 | 424 | self.manager.apps[GUI_TK] = app |
|
426 | 425 | return app |
|
427 | 426 | |
|
428 | 427 | |
|
429 | 428 | @inputhook_manager.register('glut') |
|
430 | 429 | class GlutInputHook(InputHookBase): |
|
431 | 430 | def enable(self, app=None): |
|
432 | 431 | """Enable event loop integration with GLUT. |
|
433 | 432 | |
|
434 | 433 | Parameters |
|
435 | 434 | ---------- |
|
436 | 435 | |
|
437 | 436 | app : ignored |
|
438 | 437 | Ignored, it's only a placeholder to keep the call signature of all |
|
439 | 438 | gui activation methods consistent, which simplifies the logic of |
|
440 | 439 | supporting magics. |
|
441 | 440 | |
|
442 | 441 | Notes |
|
443 | 442 | ----- |
|
444 | 443 | |
|
445 | 444 | This methods sets the PyOS_InputHook for GLUT, which allows the GLUT to |
|
446 | 445 | integrate with terminal based applications like IPython. Due to GLUT |
|
447 | 446 | limitations, it is currently not possible to start the event loop |
|
448 | 447 | without first creating a window. You should thus not create another |
|
449 | 448 | window but use instead the created one. See 'gui-glut.py' in the |
|
450 | 449 | docs/examples/lib directory. |
|
451 | 450 | |
|
452 | 451 | The default screen mode is set to: |
|
453 | 452 | glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_DEPTH |
|
454 | 453 | """ |
|
455 | 454 | |
|
456 | 455 | import OpenGL.GLUT as glut |
|
457 | 456 | from IPython.lib.inputhookglut import glut_display_mode, \ |
|
458 | 457 | glut_close, glut_display, \ |
|
459 | 458 | glut_idle, inputhook_glut |
|
460 | 459 | |
|
461 | 460 | if GUI_GLUT not in self.manager.apps: |
|
462 | 461 | glut.glutInit( sys.argv ) |
|
463 | 462 | glut.glutInitDisplayMode( glut_display_mode ) |
|
464 | 463 | # This is specific to freeglut |
|
465 | 464 | if bool(glut.glutSetOption): |
|
466 | 465 | glut.glutSetOption( glut.GLUT_ACTION_ON_WINDOW_CLOSE, |
|
467 | 466 | glut.GLUT_ACTION_GLUTMAINLOOP_RETURNS ) |
|
468 | 467 | glut.glutCreateWindow( sys.argv[0] ) |
|
469 | 468 | glut.glutReshapeWindow( 1, 1 ) |
|
470 | 469 | glut.glutHideWindow( ) |
|
471 | 470 | glut.glutWMCloseFunc( glut_close ) |
|
472 | 471 | glut.glutDisplayFunc( glut_display ) |
|
473 | 472 | glut.glutIdleFunc( glut_idle ) |
|
474 | 473 | else: |
|
475 | 474 | glut.glutWMCloseFunc( glut_close ) |
|
476 | 475 | glut.glutDisplayFunc( glut_display ) |
|
477 | 476 | glut.glutIdleFunc( glut_idle) |
|
478 | 477 | self.manager.set_inputhook( inputhook_glut ) |
|
479 | 478 | self.manager.apps[GUI_GLUT] = True |
|
480 | 479 | |
|
481 | 480 | |
|
482 | 481 | def disable(self): |
|
483 | 482 | """Disable event loop integration with glut. |
|
484 | 483 | |
|
485 | 484 | This sets PyOS_InputHook to NULL and set the display function to a |
|
486 | 485 | dummy one and set the timer to a dummy timer that will be triggered |
|
487 | 486 | very far in the future. |
|
488 | 487 | """ |
|
489 | 488 | import OpenGL.GLUT as glut |
|
490 | 489 | from glut_support import glutMainLoopEvent |
|
491 | 490 | |
|
492 | 491 | glut.glutHideWindow() # This is an event to be processed below |
|
493 | 492 | glutMainLoopEvent() |
|
494 | 493 | super(GlutInputHook, self).disable() |
|
495 | 494 | |
|
496 | 495 | @inputhook_manager.register('pyglet') |
|
497 | 496 | class PygletInputHook(InputHookBase): |
|
498 | 497 | def enable(self, app=None): |
|
499 | 498 | """Enable event loop integration with pyglet. |
|
500 | 499 | |
|
501 | 500 | Parameters |
|
502 | 501 | ---------- |
|
503 | 502 | app : ignored |
|
504 | 503 | Ignored, it's only a placeholder to keep the call signature of all |
|
505 | 504 | gui activation methods consistent, which simplifies the logic of |
|
506 | 505 | supporting magics. |
|
507 | 506 | |
|
508 | 507 | Notes |
|
509 | 508 | ----- |
|
510 | 509 | This methods sets the ``PyOS_InputHook`` for pyglet, which allows |
|
511 | 510 | pyglet to integrate with terminal based applications like |
|
512 | 511 | IPython. |
|
513 | 512 | |
|
514 | 513 | """ |
|
515 | 514 | from IPython.lib.inputhookpyglet import inputhook_pyglet |
|
516 | 515 | self.manager.set_inputhook(inputhook_pyglet) |
|
517 | 516 | return app |
|
518 | 517 | |
|
519 | 518 | |
|
520 | 519 | @inputhook_manager.register('gtk3') |
|
521 | 520 | class Gtk3InputHook(InputHookBase): |
|
522 | 521 | def enable(self, app=None): |
|
523 | 522 | """Enable event loop integration with Gtk3 (gir bindings). |
|
524 | 523 | |
|
525 | 524 | Parameters |
|
526 | 525 | ---------- |
|
527 | 526 | app : ignored |
|
528 | 527 | Ignored, it's only a placeholder to keep the call signature of all |
|
529 | 528 | gui activation methods consistent, which simplifies the logic of |
|
530 | 529 | supporting magics. |
|
531 | 530 | |
|
532 | 531 | Notes |
|
533 | 532 | ----- |
|
534 | 533 | This methods sets the PyOS_InputHook for Gtk3, which allows |
|
535 | 534 | the Gtk3 to integrate with terminal based applications like |
|
536 | 535 | IPython. |
|
537 | 536 | """ |
|
538 | 537 | from IPython.lib.inputhookgtk3 import inputhook_gtk3 |
|
539 | 538 | self.manager.set_inputhook(inputhook_gtk3) |
|
540 | 539 | self.manager._current_gui = GUI_GTK |
|
541 | 540 | |
|
542 | 541 | |
|
543 | 542 | clear_inputhook = inputhook_manager.clear_inputhook |
|
544 | 543 | set_inputhook = inputhook_manager.set_inputhook |
|
545 | 544 | current_gui = inputhook_manager.current_gui |
|
546 | 545 | clear_app_refs = inputhook_manager.clear_app_refs |
|
547 | 546 | enable_gui = inputhook_manager.enable_gui |
|
548 | 547 | disable_gui = inputhook_manager.disable_gui |
|
549 | 548 | register = inputhook_manager.register |
|
550 | 549 | guis = inputhook_manager.guihooks |
|
551 | 550 | |
|
552 | 551 | # Deprecated methods: kept for backwards compatibility, do not use in new code |
|
553 | 552 | enable_wx = lambda app=None: inputhook_manager.enable_gui('wx', app) |
|
554 | 553 | enable_qt4 = lambda app=None: inputhook_manager.enable_gui('qt4', app) |
|
555 | 554 | enable_gtk = lambda app=None: inputhook_manager.enable_gui('gtk', app) |
|
556 | 555 | enable_tk = lambda app=None: inputhook_manager.enable_gui('tk', app) |
|
557 | 556 | enable_glut = lambda app=None: inputhook_manager.enable_gui('glut', app) |
|
558 | 557 | enable_pyglet = lambda app=None: inputhook_manager.enable_gui('pyglet', app) |
|
559 | 558 | enable_gtk3 = lambda app=None: inputhook_manager.enable_gui('gtk3', app) |
|
560 | 559 | disable_wx = disable_qt4 = disable_gtk = disable_gtk3 = disable_glut = \ |
|
561 | 560 | disable_pyglet = inputhook_manager.disable_gui No newline at end of file |
@@ -1,32 +1,33 b'' | |||
|
1 | 1 | .. _config_index: |
|
2 | 2 | |
|
3 | 3 | =============================== |
|
4 | 4 | Configuration and customization |
|
5 | 5 | =============================== |
|
6 | 6 | |
|
7 | 7 | Configuring IPython |
|
8 | 8 | ------------------- |
|
9 | 9 | |
|
10 | 10 | .. toctree:: |
|
11 | 11 | :maxdepth: 2 |
|
12 | 12 | |
|
13 | 13 | intro |
|
14 | 14 | options/index |
|
15 | 15 | details |
|
16 | 16 | |
|
17 | 17 | .. seealso:: |
|
18 | 18 | |
|
19 | 19 | :doc:`/development/config` |
|
20 | 20 | Technical details of the config system. |
|
21 | 21 | |
|
22 | 22 | Extending and integrating with IPython |
|
23 | 23 | -------------------------------------- |
|
24 | 24 | |
|
25 | 25 | .. toctree:: |
|
26 | 26 | :maxdepth: 2 |
|
27 | 27 | |
|
28 | 28 | extensions/index |
|
29 | 29 | integrating |
|
30 | 30 | custommagics |
|
31 | 31 | inputtransforms |
|
32 | 32 | callbacks |
|
33 | eventloops |
General Comments 0
You need to be logged in to leave comments.
Login now