##// END OF EJS Templates
Merge pull request #5666 from takluyver/inputhook-extensible...
Min RK -
r17903:c8c43c8a merge
parent child Browse files
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 notifier = QtCore.QSocketNotifier(fd, QtCore.QSocketNotifier.Read, kernel.app)
51 notifier = QtCore.QSocketNotifier(fd, QtCore.QSocketNotifier.Read, kernel.app)
52 notifier.activated.connect(process_stream_events)
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 def loop_qt4(kernel):
82 def loop_qt4(kernel):
55 """Start a kernel with PyQt4 event loop integration."""
83 """Start a kernel with PyQt4 event loop integration."""
56
84
@@ -65,6 +93,7 b' def loop_qt4(kernel):'
65 start_event_loop_qt4(kernel.app)
93 start_event_loop_qt4(kernel.app)
66
94
67
95
96 @register_integration('wx')
68 def loop_wx(kernel):
97 def loop_wx(kernel):
69 """Start a kernel with wx event loop support."""
98 """Start a kernel with wx event loop support."""
70
99
@@ -117,6 +146,7 b' def loop_wx(kernel):'
117 start_event_loop_wx(kernel.app)
146 start_event_loop_wx(kernel.app)
118
147
119
148
149 @register_integration('tk')
120 def loop_tk(kernel):
150 def loop_tk(kernel):
121 """Start a kernel with the Tk event loop."""
151 """Start a kernel with the Tk event loop."""
122
152
@@ -146,6 +176,7 b' def loop_tk(kernel):'
146 kernel.timer.start()
176 kernel.timer.start()
147
177
148
178
179 @register_integration('gtk')
149 def loop_gtk(kernel):
180 def loop_gtk(kernel):
150 """Start the kernel, coordinating with the GTK event loop"""
181 """Start the kernel, coordinating with the GTK event loop"""
151 from .gui.gtkembed import GTKEmbed
182 from .gui.gtkembed import GTKEmbed
@@ -154,6 +185,7 b' def loop_gtk(kernel):'
154 gtk_kernel.start()
185 gtk_kernel.start()
155
186
156
187
188 @register_integration('gtk3')
157 def loop_gtk3(kernel):
189 def loop_gtk3(kernel):
158 """Start the kernel, coordinating with the GTK event loop"""
190 """Start the kernel, coordinating with the GTK event loop"""
159 from .gui.gtk3embed import GTKEmbed
191 from .gui.gtk3embed import GTKEmbed
@@ -162,6 +194,7 b' def loop_gtk3(kernel):'
162 gtk_kernel.start()
194 gtk_kernel.start()
163
195
164
196
197 @register_integration('osx')
165 def loop_cocoa(kernel):
198 def loop_cocoa(kernel):
166 """Start the kernel, coordinating with the Cocoa CFRunLoop event loop
199 """Start the kernel, coordinating with the Cocoa CFRunLoop event loop
167 via the matplotlib MacOSX backend.
200 via the matplotlib MacOSX backend.
@@ -232,18 +265,6 b' def loop_cocoa(kernel):'
232 # ensure excepthook is restored
265 # ensure excepthook is restored
233 sys.excepthook = real_excepthook
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 def enable_gui(gui, kernel=None):
270 def enable_gui(gui, kernel=None):
@@ -110,7 +110,9 b' class InputHookManager(object):'
110 warn("IPython GUI event loop requires ctypes, %gui will not be available")
110 warn("IPython GUI event loop requires ctypes, %gui will not be available")
111 return
111 return
112 self.PYFUNC = ctypes.PYFUNCTYPE(ctypes.c_int)
112 self.PYFUNC = ctypes.PYFUNCTYPE(ctypes.c_int)
113 self._apps = {}
113 self.guihooks = {}
114 self.aliases = {}
115 self.apps = {}
114 self._reset()
116 self._reset()
115
117
116 def _reset(self):
118 def _reset(self):
@@ -177,11 +179,104 b' class InputHookManager(object):'
177 as those toolkits don't have the notion of an app.
179 as those toolkits don't have the notion of an app.
178 """
180 """
179 if gui is None:
181 if gui is None:
180 self._apps = {}
182 self.apps = {}
181 elif gui in self._apps:
183 elif gui in self.apps:
182 del self._apps[gui]
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 """Enable event loop integration with wxPython.
280 """Enable event loop integration with wxPython.
186
281
187 Parameters
282 Parameters
@@ -212,30 +307,29 b' class InputHookManager(object):'
212
307
213 from IPython.lib.inputhookwx import inputhook_wx
308 from IPython.lib.inputhookwx import inputhook_wx
214 from IPython.external.appnope import nope
309 from IPython.external.appnope import nope
215 self.set_inputhook(inputhook_wx)
310 self.manager.set_inputhook(inputhook_wx)
216 nope()
311 nope()
217 self._current_gui = GUI_WX
312
218 import wx
313 import wx
219 if app is None:
314 if app is None:
220 app = wx.GetApp()
315 app = wx.GetApp()
221 if app is None:
316 if app is None:
222 app = wx.App(redirect=False, clearSigInt=False)
317 app = wx.App(redirect=False, clearSigInt=False)
223 app._in_event_loop = True
318 app._in_event_loop = True
224 self._apps[GUI_WX] = app
319 self.manager.apps[GUI_WX] = app
225 return app
320 return app
226
321
227 def disable_wx(self):
322 def disable(self):
228 """Disable event loop integration with wxPython.
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 from IPython.external.appnope import nap
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 nap()
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 """Enable event loop integration with PyQt4.
333 """Enable event loop integration with PyQt4.
240
334
241 Parameters
335 Parameters
@@ -260,26 +354,24 b' class InputHookManager(object):'
260 from IPython.lib.inputhookqt4 import create_inputhook_qt4
354 from IPython.lib.inputhookqt4 import create_inputhook_qt4
261 from IPython.external.appnope import nope
355 from IPython.external.appnope import nope
262 app, inputhook_qt4 = create_inputhook_qt4(self, app)
356 app, inputhook_qt4 = create_inputhook_qt4(self, app)
263 self.set_inputhook(inputhook_qt4)
357 self.manager.set_inputhook(inputhook_qt4)
264 nope()
358 nope()
265
359
266 self._current_gui = GUI_QT4
267 app._in_event_loop = True
360 app._in_event_loop = True
268 self._apps[GUI_QT4] = app
361 self.manager.apps[GUI_QT4] = app
269 return app
362 return app
270
363
271 def disable_qt4(self):
364 def disable_qt4(self):
272 """Disable event loop integration with PyQt4.
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 from IPython.external.appnope import nap
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 nap()
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 """Enable event loop integration with PyGTK.
375 """Enable event loop integration with PyGTK.
284
376
285 Parameters
377 Parameters
@@ -298,21 +390,15 b' class InputHookManager(object):'
298 import gtk
390 import gtk
299 try:
391 try:
300 gtk.set_interactive(True)
392 gtk.set_interactive(True)
301 self._current_gui = GUI_GTK
302 except AttributeError:
393 except AttributeError:
303 # For older versions of gtk, use our own ctypes version
394 # For older versions of gtk, use our own ctypes version
304 from IPython.lib.inputhookgtk import inputhook_gtk
395 from IPython.lib.inputhookgtk import inputhook_gtk
305 self.set_inputhook(inputhook_gtk)
396 self.manager.set_inputhook(inputhook_gtk)
306 self._current_gui = GUI_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 """Enable event loop integration with Tk.
402 """Enable event loop integration with Tk.
317
403
318 Parameters
404 Parameters
@@ -328,7 +414,6 b' class InputHookManager(object):'
328 :class:`InputHookManager`, since creating that object automatically
414 :class:`InputHookManager`, since creating that object automatically
329 sets ``PyOS_InputHook``.
415 sets ``PyOS_InputHook``.
330 """
416 """
331 self._current_gui = GUI_TK
332 if app is None:
417 if app is None:
333 try:
418 try:
334 from tkinter import Tk # Py 3
419 from tkinter import Tk # Py 3
@@ -336,19 +421,14 b' class InputHookManager(object):'
336 from Tkinter import Tk # Py 2
421 from Tkinter import Tk # Py 2
337 app = Tk()
422 app = Tk()
338 app.withdraw()
423 app.withdraw()
339 self._apps[GUI_TK] = app
424 self.manager.apps[GUI_TK] = app
340 return app
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):
428 @inputhook_manager.register('glut')
351 """ Enable event loop integration with GLUT.
429 class GlutInputHook(InputHookBase):
430 def enable(self, app=None):
431 """Enable event loop integration with GLUT.
352
432
353 Parameters
433 Parameters
354 ----------
434 ----------
@@ -377,7 +457,7 b' class InputHookManager(object):'
377 glut_close, glut_display, \
457 glut_close, glut_display, \
378 glut_idle, inputhook_glut
458 glut_idle, inputhook_glut
379
459
380 if GUI_GLUT not in self._apps:
460 if GUI_GLUT not in self.manager.apps:
381 glut.glutInit( sys.argv )
461 glut.glutInit( sys.argv )
382 glut.glutInitDisplayMode( glut_display_mode )
462 glut.glutInitDisplayMode( glut_display_mode )
383 # This is specific to freeglut
463 # This is specific to freeglut
@@ -394,12 +474,11 b' class InputHookManager(object):'
394 glut.glutWMCloseFunc( glut_close )
474 glut.glutWMCloseFunc( glut_close )
395 glut.glutDisplayFunc( glut_display )
475 glut.glutDisplayFunc( glut_display )
396 glut.glutIdleFunc( glut_idle)
476 glut.glutIdleFunc( glut_idle)
397 self.set_inputhook( inputhook_glut )
477 self.manager.set_inputhook( inputhook_glut )
398 self._current_gui = GUI_GLUT
478 self.manager.apps[GUI_GLUT] = True
399 self._apps[GUI_GLUT] = True
400
479
401
480
402 def disable_glut(self):
481 def disable(self):
403 """Disable event loop integration with glut.
482 """Disable event loop integration with glut.
404
483
405 This sets PyOS_InputHook to NULL and set the display function to a
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 glut.glutHideWindow() # This is an event to be processed below
491 glut.glutHideWindow() # This is an event to be processed below
413 glutMainLoopEvent()
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 """Enable event loop integration with pyglet.
498 """Enable event loop integration with pyglet.
418
499
419 Parameters
500 Parameters
@@ -431,18 +512,13 b' class InputHookManager(object):'
431
512
432 """
513 """
433 from IPython.lib.inputhookpyglet import inputhook_pyglet
514 from IPython.lib.inputhookpyglet import inputhook_pyglet
434 self.set_inputhook(inputhook_pyglet)
515 self.manager.set_inputhook(inputhook_pyglet)
435 self._current_gui = GUI_PYGLET
436 return app
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.
519 @inputhook_manager.register('gtk3')
442 """
520 class Gtk3InputHook(InputHookBase):
443 self.clear_inputhook()
521 def enable(self, app=None):
444
445 def enable_gtk3(self, app=None):
446 """Enable event loop integration with Gtk3 (gir bindings).
522 """Enable event loop integration with Gtk3 (gir bindings).
447
523
448 Parameters
524 Parameters
@@ -459,84 +535,35 b' class InputHookManager(object):'
459 IPython.
535 IPython.
460 """
536 """
461 from IPython.lib.inputhookgtk3 import inputhook_gtk3
537 from IPython.lib.inputhookgtk3 import inputhook_gtk3
462 self.set_inputhook(inputhook_gtk3)
538 self.manager.set_inputhook(inputhook_gtk3)
463 self._current_gui = GUI_GTK
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 clear_inputhook = inputhook_manager.clear_inputhook
542 clear_inputhook = inputhook_manager.clear_inputhook
493 set_inputhook = inputhook_manager.set_inputhook
543 set_inputhook = inputhook_manager.set_inputhook
494 current_gui = inputhook_manager.current_gui
544 current_gui = inputhook_manager.current_gui
495 clear_app_refs = inputhook_manager.clear_app_refs
545 clear_app_refs = inputhook_manager.clear_app_refs
496
546 enable_gui = inputhook_manager.enable_gui
497 guis = {None: clear_inputhook,
547 disable_gui = inputhook_manager.disable_gui
498 GUI_NONE: clear_inputhook,
548 register = inputhook_manager.register
499 GUI_OSX: lambda app=False: None,
549 guis = inputhook_manager.guihooks
500 GUI_TK: enable_tk,
550
501 GUI_GTK: enable_gtk,
551 # Deprecated methods: kept for backwards compatibility, do not use in new code
502 GUI_WX: enable_wx,
552 def _make_deprecated_enable(name):
503 GUI_QT: enable_qt4, # qt3 not supported
553 def enable_toolkit(app=None):
504 GUI_QT4: enable_qt4,
554 warn("This function is deprecated - use enable_gui(%r) instead" % name)
505 GUI_GLUT: enable_glut,
555 inputhook_manager.enable_gui(name, app)
506 GUI_PYGLET: enable_pyglet,
556
507 GUI_GTK3: enable_gtk3,
557 enable_wx = _make_deprecated_enable('wx')
508 }
558 enable_qt4 = _make_deprecated_enable('qt4')
509
559 enable_gtk = _make_deprecated_enable('gtk')
510
560 enable_tk = _make_deprecated_enable('tk')
511 # Convenience function to switch amongst them
561 enable_glut = _make_deprecated_enable('glut')
512 def enable_gui(gui=None, app=None):
562 enable_pyglet = _make_deprecated_enable('pyglet')
513 """Switch amongst GUI input hooks by name.
563 enable_gtk3 = _make_deprecated_enable('gtk3')
514
564
515 This is just a utility wrapper around the methods of the InputHookManager
565 def _deprecated_disable():
516 object.
566 warn("This function is deprecated: use disable_gui() instead")
517
567 inputhook_manager.disable_gui()
518 Parameters
568 disable_wx = disable_qt4 = disable_gtk = disable_gtk3 = disable_glut = \
519 ----------
569 disable_pyglet = _deprecated_disable
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
@@ -30,3 +30,4 b' Extending and integrating with IPython'
30 custommagics
30 custommagics
31 inputtransforms
31 inputtransforms
32 callbacks
32 callbacks
33 eventloops
@@ -33,7 +33,7 b' button.show()'
33 window.show()
33 window.show()
34
34
35 try:
35 try:
36 from IPython.lib.inputhook import enable_gtk
36 from IPython.lib.inputhook import enable_gui
37 enable_gtk()
37 enable_gui('gtk')
38 except ImportError:
38 except ImportError:
39 gtk.main()
39 gtk.main()
@@ -31,7 +31,7 b' button.show()'
31 window.show()
31 window.show()
32
32
33 try:
33 try:
34 from IPython.lib.inputhook import enable_gtk3
34 from IPython.lib.inputhook import enable_gui
35 enable_gtk3()
35 enable_gui('gtk3')
36 except ImportError:
36 except ImportError:
37 Gtk.main()
37 Gtk.main()
@@ -27,7 +27,7 b' def on_draw():'
27 label.draw()
27 label.draw()
28
28
29 try:
29 try:
30 from IPython.lib.inputhook import enable_pyglet
30 from IPython.lib.inputhook import enable_gui
31 enable_pyglet()
31 enable_gui('pyglet')
32 except ImportError:
32 except ImportError:
33 pyglet.app.run()
33 pyglet.app.run()
@@ -30,6 +30,7 b' root = Tk()'
30 app = MyApp(root)
30 app = MyApp(root)
31
31
32 try:
32 try:
33 from IPython.lib.inputhook import enable_tk; enable_tk(root)
33 from IPython.lib.inputhook import enable_gui
34 enable_gui('tk', root)
34 except ImportError:
35 except ImportError:
35 root.mainloop()
36 root.mainloop()
@@ -100,7 +100,7 b" if __name__ == '__main__':"
100 frame.Show(True)
100 frame.Show(True)
101
101
102 try:
102 try:
103 from IPython.lib.inputhook import enable_wx
103 from IPython.lib.inputhook import enable_gui
104 enable_wx(app)
104 enable_gui('wx', app)
105 except ImportError:
105 except ImportError:
106 app.MainLoop()
106 app.MainLoop()
General Comments 0
You need to be logged in to leave comments. Login now