IPython Documentation

Table Of Contents

Previous topic

lib.demo

Next topic

lib.inputhookgtk

This Page

lib.inputhook

Module: lib.inputhook

Inheritance diagram for IPython.lib.inputhook:

Inputhook management for GUI event loop integration.

Class

InputHookManager

class IPython.lib.inputhook.InputHookManager

Bases: object

Manage PyOS_InputHook for different GUI toolkits.

This class installs various hooks under PyOSInputHook to handle GUI event loop integration.

__init__()
clear_app_refs(gui=None)

Clear IPython’s internal reference to an application instance.

Whenever we create an app for a user on qt4 or wx, we hold a reference to the app. This is needed because in some cases bad things can happen if a user doesn’t hold a reference themselves. This method is provided to clear the references we are holding.

Parameters:

gui : None or str

If None, clear all app references. If (‘wx’, ‘qt4’) clear the app for that toolkit. References are not held for gtk or tk as those toolkits don’t have the notion of an app.

clear_inputhook(app=None)

Set PyOS_InputHook to NULL and return the previous one.

Parameters:

app : optional, ignored

This parameter is allowed only so that clear_inputhook() can be called with a similar interface as all the enable_* methods. But the actual value of the parameter is ignored. This uniform interface makes it easier to have user-level entry points in the main IPython app like enable_gui().

current_gui()
Return a string indicating the currently active GUI or None.
disable_gtk()

Disable event loop integration with PyGTK.

This merely sets PyOS_InputHook to NULL.

disable_qt4()

Disable event loop integration with PyQt4.

This merely sets PyOS_InputHook to NULL.

disable_tk()

Disable event loop integration with Tkinter.

This merely sets PyOS_InputHook to NULL.

disable_wx()

Disable event loop integration with wxPython.

This merely sets PyOS_InputHook to NULL.

enable_gtk(app=False)

Enable event loop integration with PyGTK.

Parameters:

app : bool

Create a running application object or not. Because gtk does’t have an app class, this does nothing.

Notes

This methods sets the PyOS_InputHook for PyGTK, which allows the PyGTK to integrate with terminal based applications like IPython.

enable_qt4(app=False)

Enable event loop integration with PyQt4.

Parameters:

app : bool

Create a running application object or not.

Notes

This methods sets the PyOS_InputHook for PyQt4, which allows the PyQt4 to integrate with terminal based applications like IPython.

If app is True, we create an QApplication as follows:

from PyQt4 import QtCore
app = QtGui.QApplication(sys.argv)

But, we first check to see if an application has already been created. If so, we simply return that instance.

enable_tk(app=False)

Enable event loop integration with Tk.

Parameters:

app : bool

Create a running application object or not.

Notes

Currently this is a no-op as creating a Tkinter.Tk object sets PyOS_InputHook.

enable_wx(app=False)

Enable event loop integration with wxPython.

Parameters:

app : bool

Create a running application object or not.

Notes

This methods sets the PyOS_InputHook for wxPython, which allows the wxPython to integrate with terminal based applications like IPython.

If app is True, we create an wx.App as follows:

import wx
app = wx.App(redirect=False, clearSigInt=False)

Both options this constructor are important for things to work properly in an interactive context.

But, we first check to see if an application has already been created. If so, we simply return that instance.

get_pyos_inputhook()
Return the current PyOS_InputHook as a ctypes.c_void_p.
get_pyos_inputhook_as_func()
Return the current PyOS_InputHook as a ctypes.PYFUNCYPE.
set_inputhook(callback)
Set PyOS_InputHook to callback and return the previous one.
spin()

Process pending events in the current gui.

This method is just provided for IPython to use internally if needed for things like testing. Third party projects should not call this method, but instead should call the underlying GUI toolkit methods that we are calling.

Functions

IPython.lib.inputhook.appstart_gtk()

Start the gtk event loop in a way that plays with IPython.

When a gtk app is run interactively in IPython, the event loop should not be started. This function checks to see if IPython’s gtk integration is activated and if so, it passes. If not, it will call gtk.main(). Unlike the other appstart implementations, this does not take an app argument.

This function should be used by users who want their gtk scripts to work both at the command line and in IPython. These users should put the following logic at the bottom on their script:

try:
from IPython.lib.inputhook import appstart_gtk appstart_gtk()
except ImportError:
gtk.main()
IPython.lib.inputhook.appstart_qt4(app)

Start the qt4 event loop in a way that plays with IPython.

When a qt4 app is run interactively in IPython, the event loop should not be started. This function checks to see if IPython’s qt4 integration is activated and if so, it passes. If not, it will call the exec_() method of the main qt4 app.

This function should be used by users who want their qt4 scripts to work both at the command line and in IPython. These users should put the following logic at the bottom on their script, after they create a QApplication instance (called app here):

try:
from IPython.lib.inputhook import appstart_qt4 appstart_qt4(app)
except ImportError:
app.exec_()
IPython.lib.inputhook.appstart_tk(app)

Start the tk event loop in a way that plays with IPython.

When a tk app is run interactively in IPython, the event loop should not be started. This function checks to see if IPython’s tk integration is activated and if so, it passes. If not, it will call the mainloop() method of the tk object passed to this method.

This function should be used by users who want their tk scripts to work both at the command line and in IPython. These users should put the following logic at the bottom on their script, after they create a Tk instance (called app here):

try:
from IPython.lib.inputhook import appstart_tk appstart_tk(app)
except ImportError:
app.mainloop()
IPython.lib.inputhook.appstart_wx(app)

Start the wx event loop in a way that plays with IPython.

When a wx app is run interactively in IPython, the event loop should not be started. This function checks to see if IPython’s wx integration is activated and if so, it passes. If not, it will call the MainLoop() method of the main qt4 app.

This function should be used by users who want their wx scripts to work both at the command line and in IPython. These users should put the following logic at the bottom on their script, after they create a App instance (called app here):

try:
from IPython.lib.inputhook import appstart_wx appstart_wx(app)
except ImportError:
app.MainLoop()
IPython.lib.inputhook.enable_gui(gui=None, app=True)

Switch amongst GUI input hooks by name.

This is just a utility wrapper around the methods of the InputHookManager object.

Parameters:

gui : optional, string or None

If None, clears input hook, otherwise it must be one of the recognized GUI names (see GUI_* constants in module).

app : optional, bool

If true, create an app object and return it.

Returns:

The output of the underlying gui switch routine, typically the actual :

PyOS_InputHook wrapper object or the GUI toolkit app created, if there was :

one. :