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,7 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. As part | |
|
5 | of this, the direct ``enable_*`` and ``disable_*`` functions for various GUIs | |
|
6 | in :mod:`IPython.lib.inputhook` have been deprecated in favour of | |
|
7 | :meth:`~.InputHookManager.enable_gui` and :meth:`~.InputHookManager.disable_gui`. |
@@ -51,6 +51,34 b' def _notify_stream_qt(kernel, stream):' | |||
|
51 | 51 | notifier = QtCore.QSocketNotifier(fd, QtCore.QSocketNotifier.Read, kernel.app) |
|
52 | 52 | notifier.activated.connect(process_stream_events) |
|
53 | 53 | |
|
54 | # mapping of keys to loop functions | |
|
55 | loop_map = { | |
|
56 | 'inline': None, | |
|
57 | None : None, | |
|
58 | } | |
|
59 | ||
|
60 | def register_integration(*toolkitnames): | |
|
61 | """Decorator to register an event loop to integrate with the IPython kernel | |
|
62 | ||
|
63 | The decorator takes names to register the event loop as for the %gui magic. | |
|
64 | You can provide alternative names for the same toolkit. | |
|
65 | ||
|
66 | The decorated function should take a single argument, the IPython kernel | |
|
67 | instance, arrange for the event loop to call ``kernel.do_one_iteration()`` | |
|
68 | at least every ``kernel._poll_interval`` seconds, and start the event loop. | |
|
69 | ||
|
70 | :mod:`IPython.kernel.zmq.eventloops` provides and registers such functions | |
|
71 | for a few common event loops. | |
|
72 | """ | |
|
73 | def decorator(func): | |
|
74 | for name in toolkitnames: | |
|
75 | loop_map[name] = func | |
|
76 | return func | |
|
77 | ||
|
78 | return decorator | |
|
79 | ||
|
80 | ||
|
81 | @register_integration('qt', 'qt4') | |
|
54 | 82 | def loop_qt4(kernel): |
|
55 | 83 | """Start a kernel with PyQt4 event loop integration.""" |
|
56 | 84 | |
@@ -65,6 +93,7 b' def loop_qt4(kernel):' | |||
|
65 | 93 | start_event_loop_qt4(kernel.app) |
|
66 | 94 | |
|
67 | 95 | |
|
96 | @register_integration('wx') | |
|
68 | 97 | def loop_wx(kernel): |
|
69 | 98 | """Start a kernel with wx event loop support.""" |
|
70 | 99 | |
@@ -117,6 +146,7 b' def loop_wx(kernel):' | |||
|
117 | 146 | start_event_loop_wx(kernel.app) |
|
118 | 147 | |
|
119 | 148 | |
|
149 | @register_integration('tk') | |
|
120 | 150 | def loop_tk(kernel): |
|
121 | 151 | """Start a kernel with the Tk event loop.""" |
|
122 | 152 | |
@@ -146,6 +176,7 b' def loop_tk(kernel):' | |||
|
146 | 176 | kernel.timer.start() |
|
147 | 177 | |
|
148 | 178 | |
|
179 | @register_integration('gtk') | |
|
149 | 180 | def loop_gtk(kernel): |
|
150 | 181 | """Start the kernel, coordinating with the GTK event loop""" |
|
151 | 182 | from .gui.gtkembed import GTKEmbed |
@@ -154,6 +185,7 b' def loop_gtk(kernel):' | |||
|
154 | 185 | gtk_kernel.start() |
|
155 | 186 | |
|
156 | 187 | |
|
188 | @register_integration('gtk3') | |
|
157 | 189 | def loop_gtk3(kernel): |
|
158 | 190 | """Start the kernel, coordinating with the GTK event loop""" |
|
159 | 191 | from .gui.gtk3embed import GTKEmbed |
@@ -162,6 +194,7 b' def loop_gtk3(kernel):' | |||
|
162 | 194 | gtk_kernel.start() |
|
163 | 195 | |
|
164 | 196 | |
|
197 | @register_integration('osx') | |
|
165 | 198 | def loop_cocoa(kernel): |
|
166 | 199 | """Start the kernel, coordinating with the Cocoa CFRunLoop event loop |
|
167 | 200 | via the matplotlib MacOSX backend. |
@@ -232,18 +265,6 b' def loop_cocoa(kernel):' | |||
|
232 | 265 | # ensure excepthook is restored |
|
233 | 266 | sys.excepthook = real_excepthook |
|
234 | 267 | |
|
235 | # mapping of keys to loop functions | |
|
236 | loop_map = { | |
|
237 | 'qt' : loop_qt4, | |
|
238 | 'qt4': loop_qt4, | |
|
239 | 'inline': None, | |
|
240 | 'osx': loop_cocoa, | |
|
241 | 'wx' : loop_wx, | |
|
242 | 'tk' : loop_tk, | |
|
243 | 'gtk': loop_gtk, | |
|
244 | 'gtk3': loop_gtk3, | |
|
245 | None : None, | |
|
246 | } | |
|
247 | 268 | |
|
248 | 269 | |
|
249 | 270 | def enable_gui(gui, kernel=None): |
@@ -110,7 +110,9 b' class InputHookManager(object):' | |||
|
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 |
self. |
|
|
113 | self.guihooks = {} | |
|
114 | self.aliases = {} | |
|
115 | self.apps = {} | |
|
114 | 116 | self._reset() |
|
115 | 117 | |
|
116 | 118 | def _reset(self): |
@@ -177,11 +179,104 b' class InputHookManager(object):' | |||
|
177 | 179 | as those toolkits don't have the notion of an app. |
|
178 | 180 | """ |
|
179 | 181 | if gui is None: |
|
180 |
self. |
|
|
181 |
elif gui in self. |
|
|
182 |
del self. |
|
|
182 | self.apps = {} | |
|
183 | elif gui in self.apps: | |
|
184 | del self.apps[gui] | |
|
183 | 185 | |
|
184 | def enable_wx(self, app=None): | |
|
186 | def register(self, toolkitname, *aliases): | |
|
187 | """Register a class to provide the event loop for a given GUI. | |
|
188 | ||
|
189 | This is intended to be used as a class decorator. It should be passed | |
|
190 | the names with which to register this GUI integration. The classes | |
|
191 | themselves should subclass :class:`InputHookBase`. | |
|
192 | ||
|
193 | :: | |
|
194 | ||
|
195 | @inputhook_manager.register('qt') | |
|
196 | class QtInputHook(InputHookBase): | |
|
197 | def enable(self, app=None): | |
|
198 | ... | |
|
199 | """ | |
|
200 | def decorator(cls): | |
|
201 | inst = cls(self) | |
|
202 | self.guihooks[toolkitname] = inst | |
|
203 | for a in aliases: | |
|
204 | self.aliases[a] = toolkitname | |
|
205 | return cls | |
|
206 | return decorator | |
|
207 | ||
|
208 | def current_gui(self): | |
|
209 | """Return a string indicating the currently active GUI or None.""" | |
|
210 | return self._current_gui | |
|
211 | ||
|
212 | def enable_gui(self, gui=None, app=None): | |
|
213 | """Switch amongst GUI input hooks by name. | |
|
214 | ||
|
215 | This is a higher level method than :meth:`set_inputhook` - it uses the | |
|
216 | GUI name to look up a registered object which enables the input hook | |
|
217 | for that GUI. | |
|
218 | ||
|
219 | Parameters | |
|
220 | ---------- | |
|
221 | gui : optional, string or None | |
|
222 | If None (or 'none'), clears input hook, otherwise it must be one | |
|
223 | of the recognized GUI names (see ``GUI_*`` constants in module). | |
|
224 | ||
|
225 | app : optional, existing application object. | |
|
226 | For toolkits that have the concept of a global app, you can supply an | |
|
227 | existing one. If not given, the toolkit will be probed for one, and if | |
|
228 | none is found, a new one will be created. Note that GTK does not have | |
|
229 | this concept, and passing an app if ``gui=="GTK"`` will raise an error. | |
|
230 | ||
|
231 | Returns | |
|
232 | ------- | |
|
233 | The output of the underlying gui switch routine, typically the actual | |
|
234 | PyOS_InputHook wrapper object or the GUI toolkit app created, if there was | |
|
235 | one. | |
|
236 | """ | |
|
237 | if gui in (None, GUI_NONE): | |
|
238 | return self.disable_gui() | |
|
239 | ||
|
240 | if gui in self.aliases: | |
|
241 | return self.enable_gui(self.aliases[gui], app) | |
|
242 | ||
|
243 | try: | |
|
244 | gui_hook = self.guihooks[gui] | |
|
245 | except KeyError: | |
|
246 | e = "Invalid GUI request {!r}, valid ones are: {}" | |
|
247 | raise ValueError(e.format(gui, ', '.join(self.guihooks))) | |
|
248 | self._current_gui = gui | |
|
249 | return gui_hook.enable(app) | |
|
250 | ||
|
251 | def disable_gui(self): | |
|
252 | """Disable GUI event loop integration. | |
|
253 | ||
|
254 | If an application was registered, this sets its ``_in_event_loop`` | |
|
255 | attribute to False. It then calls :meth:`clear_inputhook`. | |
|
256 | """ | |
|
257 | gui = self._current_gui | |
|
258 | if gui in self.apps: | |
|
259 | self.apps[gui]._in_event_loop = False | |
|
260 | return self.clear_inputhook() | |
|
261 | ||
|
262 | class InputHookBase(object): | |
|
263 | """Base class for input hooks for specific toolkits. | |
|
264 | ||
|
265 | Subclasses should define an :meth:`enable` method with one argument, ``app``, | |
|
266 | which will either be an instance of the toolkit's application class, or None. | |
|
267 | They may also define a :meth:`disable` method with no arguments. | |
|
268 | """ | |
|
269 | def __init__(self, manager): | |
|
270 | self.manager = manager | |
|
271 | ||
|
272 | def disable(self): | |
|
273 | pass | |
|
274 | ||
|
275 | inputhook_manager = InputHookManager() | |
|
276 | ||
|
277 | @inputhook_manager.register('wx') | |
|
278 | class WxInputHook(InputHookBase): | |
|
279 | def enable(self, app=None): | |
|
185 | 280 | """Enable event loop integration with wxPython. |
|
186 | 281 | |
|
187 | 282 | Parameters |
@@ -212,30 +307,29 b' class InputHookManager(object):' | |||
|
212 | 307 | |
|
213 | 308 | from IPython.lib.inputhookwx import inputhook_wx |
|
214 | 309 | from IPython.external.appnope import nope |
|
215 | self.set_inputhook(inputhook_wx) | |
|
310 | self.manager.set_inputhook(inputhook_wx) | |
|
216 | 311 | nope() |
|
217 | self._current_gui = GUI_WX | |
|
312 | ||
|
218 | 313 | import wx |
|
219 | 314 | if app is None: |
|
220 | 315 | app = wx.GetApp() |
|
221 | 316 | if app is None: |
|
222 | 317 | app = wx.App(redirect=False, clearSigInt=False) |
|
223 | 318 | app._in_event_loop = True |
|
224 |
self. |
|
|
319 | self.manager.apps[GUI_WX] = app | |
|
225 | 320 | return app |
|
226 | 321 | |
|
227 |
def disable |
|
|
322 | def disable(self): | |
|
228 | 323 | """Disable event loop integration with wxPython. |
|
229 | 324 | |
|
230 | This merely sets PyOS_InputHook to NULL. | |
|
325 | This restores appnapp on OS X | |
|
231 | 326 | """ |
|
232 | 327 | from IPython.external.appnope import nap |
|
233 | if GUI_WX in self._apps: | |
|
234 | self._apps[GUI_WX]._in_event_loop = False | |
|
235 | self.clear_inputhook() | |
|
236 | 328 | nap() |
|
237 | 329 | |
|
238 | def enable_qt4(self, app=None): | |
|
330 | @inputhook_manager.register('qt', 'qt4') | |
|
331 | class Qt4InputHook(InputHookBase): | |
|
332 | def enable(self, app=None): | |
|
239 | 333 | """Enable event loop integration with PyQt4. |
|
240 | 334 | |
|
241 | 335 | Parameters |
@@ -260,26 +354,24 b' class InputHookManager(object):' | |||
|
260 | 354 | from IPython.lib.inputhookqt4 import create_inputhook_qt4 |
|
261 | 355 | from IPython.external.appnope import nope |
|
262 | 356 | app, inputhook_qt4 = create_inputhook_qt4(self, app) |
|
263 | self.set_inputhook(inputhook_qt4) | |
|
357 | self.manager.set_inputhook(inputhook_qt4) | |
|
264 | 358 | nope() |
|
265 | 359 | |
|
266 | self._current_gui = GUI_QT4 | |
|
267 | 360 | app._in_event_loop = True |
|
268 |
self. |
|
|
361 | self.manager.apps[GUI_QT4] = app | |
|
269 | 362 | return app |
|
270 | 363 | |
|
271 | 364 | def disable_qt4(self): |
|
272 | 365 | """Disable event loop integration with PyQt4. |
|
273 | 366 | |
|
274 | This merely sets PyOS_InputHook to NULL. | |
|
367 | This restores appnapp on OS X | |
|
275 | 368 | """ |
|
276 | 369 | from IPython.external.appnope import nap |
|
277 | if GUI_QT4 in self._apps: | |
|
278 | self._apps[GUI_QT4]._in_event_loop = False | |
|
279 | self.clear_inputhook() | |
|
280 | 370 | nap() |
|
281 | 371 | |
|
282 | def enable_gtk(self, app=None): | |
|
372 | @inputhook_manager.register('gtk') | |
|
373 | class GtkInputHook(InputHookBase): | |
|
374 | def enable(self, app=None): | |
|
283 | 375 | """Enable event loop integration with PyGTK. |
|
284 | 376 | |
|
285 | 377 | Parameters |
@@ -298,21 +390,15 b' class InputHookManager(object):' | |||
|
298 | 390 | import gtk |
|
299 | 391 | try: |
|
300 | 392 | gtk.set_interactive(True) |
|
301 | self._current_gui = GUI_GTK | |
|
302 | 393 | except AttributeError: |
|
303 | 394 | # For older versions of gtk, use our own ctypes version |
|
304 | 395 | from IPython.lib.inputhookgtk import inputhook_gtk |
|
305 | self.set_inputhook(inputhook_gtk) | |
|
306 | self._current_gui = GUI_GTK | |
|
396 | self.manager.set_inputhook(inputhook_gtk) | |
|
307 | 397 | |
|
308 | def disable_gtk(self): | |
|
309 | """Disable event loop integration with PyGTK. | |
|
310 | ||
|
311 | This merely sets PyOS_InputHook to NULL. | |
|
312 | """ | |
|
313 | self.clear_inputhook() | |
|
314 | 398 | |
|
315 | def enable_tk(self, app=None): | |
|
399 | @inputhook_manager.register('tk') | |
|
400 | class TkInputHook(InputHookBase): | |
|
401 | def enable(self, app=None): | |
|
316 | 402 | """Enable event loop integration with Tk. |
|
317 | 403 | |
|
318 | 404 | Parameters |
@@ -328,7 +414,6 b' class InputHookManager(object):' | |||
|
328 | 414 | :class:`InputHookManager`, since creating that object automatically |
|
329 | 415 | sets ``PyOS_InputHook``. |
|
330 | 416 | """ |
|
331 | self._current_gui = GUI_TK | |
|
332 | 417 | if app is None: |
|
333 | 418 | try: |
|
334 | 419 | from tkinter import Tk # Py 3 |
@@ -336,19 +421,14 b' class InputHookManager(object):' | |||
|
336 | 421 | from Tkinter import Tk # Py 2 |
|
337 | 422 | app = Tk() |
|
338 | 423 | app.withdraw() |
|
339 |
self. |
|
|
424 | self.manager.apps[GUI_TK] = app | |
|
340 | 425 | return app |
|
341 | 426 | |
|
342 | def disable_tk(self): | |
|
343 | """Disable event loop integration with Tkinter. | |
|
344 | ||
|
345 | This merely sets PyOS_InputHook to NULL. | |
|
346 | """ | |
|
347 | self.clear_inputhook() | |
|
348 | ||
|
349 | 427 | |
|
350 | def enable_glut(self, app=None): | |
|
351 | """ Enable event loop integration with GLUT. | |
|
428 | @inputhook_manager.register('glut') | |
|
429 | class GlutInputHook(InputHookBase): | |
|
430 | def enable(self, app=None): | |
|
431 | """Enable event loop integration with GLUT. | |
|
352 | 432 | |
|
353 | 433 | Parameters |
|
354 | 434 | ---------- |
@@ -377,7 +457,7 b' class InputHookManager(object):' | |||
|
377 | 457 | glut_close, glut_display, \ |
|
378 | 458 | glut_idle, inputhook_glut |
|
379 | 459 | |
|
380 |
if GUI_GLUT not in self. |
|
|
460 | if GUI_GLUT not in self.manager.apps: | |
|
381 | 461 | glut.glutInit( sys.argv ) |
|
382 | 462 | glut.glutInitDisplayMode( glut_display_mode ) |
|
383 | 463 | # This is specific to freeglut |
@@ -394,12 +474,11 b' class InputHookManager(object):' | |||
|
394 | 474 | glut.glutWMCloseFunc( glut_close ) |
|
395 | 475 | glut.glutDisplayFunc( glut_display ) |
|
396 | 476 | glut.glutIdleFunc( glut_idle) |
|
397 | self.set_inputhook( inputhook_glut ) | |
|
398 | self._current_gui = GUI_GLUT | |
|
399 | self._apps[GUI_GLUT] = True | |
|
477 | self.manager.set_inputhook( inputhook_glut ) | |
|
478 | self.manager.apps[GUI_GLUT] = True | |
|
400 | 479 | |
|
401 | 480 | |
|
402 |
def disable |
|
|
481 | def disable(self): | |
|
403 | 482 | """Disable event loop integration with glut. |
|
404 | 483 | |
|
405 | 484 | This sets PyOS_InputHook to NULL and set the display function to a |
@@ -411,9 +490,11 b' class InputHookManager(object):' | |||
|
411 | 490 | |
|
412 | 491 | glut.glutHideWindow() # This is an event to be processed below |
|
413 | 492 | glutMainLoopEvent() |
|
414 | self.clear_inputhook() | |
|
493 | super(GlutInputHook, self).disable() | |
|
415 | 494 | |
|
416 | def enable_pyglet(self, app=None): | |
|
495 | @inputhook_manager.register('pyglet') | |
|
496 | class PygletInputHook(InputHookBase): | |
|
497 | def enable(self, app=None): | |
|
417 | 498 | """Enable event loop integration with pyglet. |
|
418 | 499 | |
|
419 | 500 | Parameters |
@@ -431,18 +512,13 b' class InputHookManager(object):' | |||
|
431 | 512 | |
|
432 | 513 | """ |
|
433 | 514 | from IPython.lib.inputhookpyglet import inputhook_pyglet |
|
434 | self.set_inputhook(inputhook_pyglet) | |
|
435 | self._current_gui = GUI_PYGLET | |
|
515 | self.manager.set_inputhook(inputhook_pyglet) | |
|
436 | 516 | return app |
|
437 | 517 | |
|
438 | def disable_pyglet(self): | |
|
439 | """Disable event loop integration with pyglet. | |
|
440 | 518 | |
|
441 | This merely sets PyOS_InputHook to NULL. | |
|
442 | """ | |
|
443 | self.clear_inputhook() | |
|
444 | ||
|
445 | def enable_gtk3(self, app=None): | |
|
519 | @inputhook_manager.register('gtk3') | |
|
520 | class Gtk3InputHook(InputHookBase): | |
|
521 | def enable(self, app=None): | |
|
446 | 522 | """Enable event loop integration with Gtk3 (gir bindings). |
|
447 | 523 | |
|
448 | 524 | Parameters |
@@ -459,84 +535,35 b' class InputHookManager(object):' | |||
|
459 | 535 | IPython. |
|
460 | 536 | """ |
|
461 | 537 | from IPython.lib.inputhookgtk3 import inputhook_gtk3 |
|
462 | self.set_inputhook(inputhook_gtk3) | |
|
463 | self._current_gui = GUI_GTK | |
|
538 | self.manager.set_inputhook(inputhook_gtk3) | |
|
539 | self.manager._current_gui = GUI_GTK | |
|
464 | 540 | |
|
465 | def disable_gtk3(self): | |
|
466 | """Disable event loop integration with PyGTK. | |
|
467 | 541 | |
|
468 | This merely sets PyOS_InputHook to NULL. | |
|
469 | """ | |
|
470 | self.clear_inputhook() | |
|
471 | ||
|
472 | def current_gui(self): | |
|
473 | """Return a string indicating the currently active GUI or None.""" | |
|
474 | return self._current_gui | |
|
475 | ||
|
476 | inputhook_manager = InputHookManager() | |
|
477 | ||
|
478 | enable_wx = inputhook_manager.enable_wx | |
|
479 | disable_wx = inputhook_manager.disable_wx | |
|
480 | enable_qt4 = inputhook_manager.enable_qt4 | |
|
481 | disable_qt4 = inputhook_manager.disable_qt4 | |
|
482 | enable_gtk = inputhook_manager.enable_gtk | |
|
483 | disable_gtk = inputhook_manager.disable_gtk | |
|
484 | enable_tk = inputhook_manager.enable_tk | |
|
485 | disable_tk = inputhook_manager.disable_tk | |
|
486 | enable_glut = inputhook_manager.enable_glut | |
|
487 | disable_glut = inputhook_manager.disable_glut | |
|
488 | enable_pyglet = inputhook_manager.enable_pyglet | |
|
489 | disable_pyglet = inputhook_manager.disable_pyglet | |
|
490 | enable_gtk3 = inputhook_manager.enable_gtk3 | |
|
491 | disable_gtk3 = inputhook_manager.disable_gtk3 | |
|
492 | 542 | clear_inputhook = inputhook_manager.clear_inputhook |
|
493 | 543 | set_inputhook = inputhook_manager.set_inputhook |
|
494 | 544 | current_gui = inputhook_manager.current_gui |
|
495 | 545 | clear_app_refs = inputhook_manager.clear_app_refs |
|
496 | ||
|
497 | guis = {None: clear_inputhook, | |
|
498 | GUI_NONE: clear_inputhook, | |
|
499 | GUI_OSX: lambda app=False: None, | |
|
500 | GUI_TK: enable_tk, | |
|
501 | GUI_GTK: enable_gtk, | |
|
502 | GUI_WX: enable_wx, | |
|
503 | GUI_QT: enable_qt4, # qt3 not supported | |
|
504 | GUI_QT4: enable_qt4, | |
|
505 | GUI_GLUT: enable_glut, | |
|
506 | GUI_PYGLET: enable_pyglet, | |
|
507 | GUI_GTK3: enable_gtk3, | |
|
508 | } | |
|
509 | ||
|
510 | ||
|
511 | # Convenience function to switch amongst them | |
|
512 | def enable_gui(gui=None, app=None): | |
|
513 | """Switch amongst GUI input hooks by name. | |
|
514 | ||
|
515 | This is just a utility wrapper around the methods of the InputHookManager | |
|
516 | object. | |
|
517 | ||
|
518 | Parameters | |
|
519 | ---------- | |
|
520 | gui : optional, string or None | |
|
521 | If None (or 'none'), clears input hook, otherwise it must be one | |
|
522 | of the recognized GUI names (see ``GUI_*`` constants in module). | |
|
523 | ||
|
524 | app : optional, existing application object. | |
|
525 | For toolkits that have the concept of a global app, you can supply an | |
|
526 | existing one. If not given, the toolkit will be probed for one, and if | |
|
527 | none is found, a new one will be created. Note that GTK does not have | |
|
528 | this concept, and passing an app if ``gui=="GTK"`` will raise an error. | |
|
529 | ||
|
530 | Returns | |
|
531 | ------- | |
|
532 | The output of the underlying gui switch routine, typically the actual | |
|
533 | PyOS_InputHook wrapper object or the GUI toolkit app created, if there was | |
|
534 | one. | |
|
535 | """ | |
|
536 | try: | |
|
537 | gui_hook = guis[gui] | |
|
538 | except KeyError: | |
|
539 | e = "Invalid GUI request %r, valid ones are:%s" % (gui, guis.keys()) | |
|
540 | raise ValueError(e) | |
|
541 | return gui_hook(app) | |
|
542 | ||
|
546 | enable_gui = inputhook_manager.enable_gui | |
|
547 | disable_gui = inputhook_manager.disable_gui | |
|
548 | register = inputhook_manager.register | |
|
549 | guis = inputhook_manager.guihooks | |
|
550 | ||
|
551 | # Deprecated methods: kept for backwards compatibility, do not use in new code | |
|
552 | def _make_deprecated_enable(name): | |
|
553 | def enable_toolkit(app=None): | |
|
554 | warn("This function is deprecated - use enable_gui(%r) instead" % name) | |
|
555 | inputhook_manager.enable_gui(name, app) | |
|
556 | ||
|
557 | enable_wx = _make_deprecated_enable('wx') | |
|
558 | enable_qt4 = _make_deprecated_enable('qt4') | |
|
559 | enable_gtk = _make_deprecated_enable('gtk') | |
|
560 | enable_tk = _make_deprecated_enable('tk') | |
|
561 | enable_glut = _make_deprecated_enable('glut') | |
|
562 | enable_pyglet = _make_deprecated_enable('pyglet') | |
|
563 | enable_gtk3 = _make_deprecated_enable('gtk3') | |
|
564 | ||
|
565 | def _deprecated_disable(): | |
|
566 | warn("This function is deprecated: use disable_gui() instead") | |
|
567 | inputhook_manager.disable_gui() | |
|
568 | disable_wx = disable_qt4 = disable_gtk = disable_gtk3 = disable_glut = \ | |
|
569 | disable_pyglet = _deprecated_disable |
@@ -30,3 +30,4 b' Extending and integrating with IPython' | |||
|
30 | 30 | custommagics |
|
31 | 31 | inputtransforms |
|
32 | 32 | callbacks |
|
33 | eventloops |
@@ -33,7 +33,7 b' button.show()' | |||
|
33 | 33 | window.show() |
|
34 | 34 | |
|
35 | 35 | try: |
|
36 |
from IPython.lib.inputhook import enable_g |
|
|
37 |
enable_g |
|
|
36 | from IPython.lib.inputhook import enable_gui | |
|
37 | enable_gui('gtk') | |
|
38 | 38 | except ImportError: |
|
39 | 39 | gtk.main() |
@@ -31,7 +31,7 b' button.show()' | |||
|
31 | 31 | window.show() |
|
32 | 32 | |
|
33 | 33 | try: |
|
34 |
from IPython.lib.inputhook import enable_g |
|
|
35 |
enable_g |
|
|
34 | from IPython.lib.inputhook import enable_gui | |
|
35 | enable_gui('gtk3') | |
|
36 | 36 | except ImportError: |
|
37 | 37 | Gtk.main() |
@@ -27,7 +27,7 b' def on_draw():' | |||
|
27 | 27 | label.draw() |
|
28 | 28 | |
|
29 | 29 | try: |
|
30 |
from IPython.lib.inputhook import enable_ |
|
|
31 |
enable_ |
|
|
30 | from IPython.lib.inputhook import enable_gui | |
|
31 | enable_gui('pyglet') | |
|
32 | 32 | except ImportError: |
|
33 | 33 | pyglet.app.run() |
General Comments 0
You need to be logged in to leave comments.
Login now