##// END OF EJS Templates
More testing and docstrings added for inputhook.py
Brian Granger -
Show More
@@ -19,7 +19,8 b' from IPython.lib.inputhook import ('
19 19 enable_wx, disable_wx,
20 20 enable_gtk, disable_gtk,
21 21 enable_qt4, disable_qt4,
22 enable_tk, disable_tk
22 enable_tk, disable_tk,
23 set_inputhook, clear_inputhook
23 24 )
24 25
25 26 #-----------------------------------------------------------------------------
@@ -23,6 +23,7 b' import ctypes'
23 23
24 24
25 25 class InputHookManager(object):
26 """Manage PyOS_InputHook for different GUI toolkits."""
26 27
27 28 def __init__(self):
28 29 self.PYFUNC = ctypes.PYFUNCTYPE(ctypes.c_int)
@@ -34,12 +35,16 b' class InputHookManager(object):'
34 35 self._installed = False
35 36
36 37 def get_pyos_inputhook(self):
38 """Return the current PyOS_InputHook as a ctypes.c_void_p.
39 """
37 40 return ctypes.c_void_p.in_dll(ctypes.pythonapi,"PyOS_InputHook")
38 41
39 42 def get_pyos_inputhook_as_func(self):
43 """Return the current PyOS_InputHook as a ctypes.PYFUNCYPE.
44 """
40 45 return self.PYFUNC.in_dll(ctypes.pythonapi,"PyOS_InputHook")
41 46
42 def set_inputhook(callback):
47 def set_inputhook(self, callback):
43 48 """Set PyOS_InputHook to callback and return the previous one.
44 49 """
45 50 self._callback = callback
@@ -52,7 +57,8 b' class InputHookManager(object):'
52 57 return original
53 58
54 59 def clear_inputhook(self):
55 """Set PyOS_InputHook to NULL and return the previous one."""
60 """Set PyOS_InputHook to NULL and return the previous one.
61 """
56 62 pyos_inputhook_ptr = self.get_pyos_inputhook()
57 63 original = self.get_pyos_inputhook_as_func()
58 64 pyos_inputhook_ptr.value = ctypes.c_void_p(None).value
@@ -60,13 +66,44 b' class InputHookManager(object):'
60 66 return original
61 67
62 68 def enable_wx(self):
69 """Enable event loop integration with wxPython.
70
71 This methods sets the PyOS_InputHook for wxPython, which allows
72 the wxPython to integrate with terminal based applications like
73 IPython.
74
75 Once this has been called, you can use wx interactively by doing::
76
77 >>> import wx
78 >>> app = wx.App(redirect=False, clearSigInt=False)
79
80 Both options this constructor are important for things to work
81 properly in an interactive context.
82
83 But, *don't start the event loop*. That is handled automatically by
84 PyOS_InputHook.
85 """
63 86 from IPython.lib.inputhookwx import inputhook_wx
64 87 self.set_inputhook(inputhook_wx)
65 88
66 89 def disable_wx(self):
90 """Disable event loop integration with wxPython.
91
92 This merely sets PyOS_InputHook to NULL.
93 """
67 94 self.clear_inputhook()
68 95
69 96 def enable_qt4(self):
97 """Enable event loop integration with PyQt4.
98
99 This methods sets the PyOS_InputHook for wxPython, which allows
100 the PyQt4 to integrate with terminal based applications like
101 IPython.
102
103 Once this has been called, you can simply create a QApplication and
104 use it. But, *don't start the event loop*. That is handled
105 automatically by PyOS_InputHook.
106 """
70 107 from PyQt4 import QtCore
71 108 # PyQt4 has had this since 4.3.1. In version 4.2, PyOS_InputHook
72 109 # was set when QtCore was imported, but if it ever got removed,
@@ -78,9 +115,23 b' class InputHookManager(object):'
78 115 pass
79 116
80 117 def disable_qt4(self):
118 """Disable event loop integration with PyQt4.
119
120 This merely sets PyOS_InputHook to NULL.
121 """
81 122 self.clear_inputhook()
82 123
83 124 def enable_gtk(self):
125 """Enable event loop integration with PyGTK.
126
127 This methods sets the PyOS_InputHook for PyGTK, which allows
128 the PyGTK to integrate with terminal based applications like
129 IPython.
130
131 Once this has been called, you can simple create PyGTK objects and
132 use them. But, *don't start the event loop*. That is handled
133 automatically by PyOS_InputHook.
134 """
84 135 import gtk
85 136 try:
86 137 gtk.set_interactive(True)
@@ -90,6 +141,10 b' class InputHookManager(object):'
90 141 add_inputhook(inputhook_gtk)
91 142
92 143 def disable_gtk(self):
144 """Disable event loop integration with PyGTK.
145
146 This merely sets PyOS_InputHook to NULL.
147 """
93 148 self.clear_inputhook()
94 149
95 150 def enable_tk(self):
@@ -97,6 +152,10 b' class InputHookManager(object):'
97 152 pass
98 153
99 154 def disable_tk(self):
155 """Disable event loop integration with Tkinter.
156
157 This merely sets PyOS_InputHook to NULL.
158 """
100 159 self.clear_inputhook()
101 160
102 161 inputhook_manager = InputHookManager()
@@ -108,4 +167,6 b' disable_qt4 = inputhook_manager.disable_qt4'
108 167 enable_gtk = inputhook_manager.enable_gtk
109 168 disable_gtk = inputhook_manager.disable_gtk
110 169 enable_tk = inputhook_manager.enable_tk
111 disable_tk = inputhook_manager.disable_tk No newline at end of file
170 disable_tk = inputhook_manager.disable_tk
171 clear_inputhook = inputhook_manager.clear_inputhook
172 set_inputhook = inputhook_manager.set_inputhook No newline at end of file
@@ -13,9 +13,17 b' Authors: Brian Granger'
13 13 # the file COPYING, distributed as part of this software.
14 14 #-----------------------------------------------------------------------------
15 15
16 #-----------------------------------------------------------------------------
17 # Imports
18 #-----------------------------------------------------------------------------
19
16 20 import sys
17 21 import gtk, gobject
18 22
23 #-----------------------------------------------------------------------------
24 # Code
25 #-----------------------------------------------------------------------------
26
19 27
20 28 def _main_quit(*args, **kwargs):
21 29 gtk.main_quit()
General Comments 0
You need to be logged in to leave comments. Login now