##// END OF EJS Templates
Make gui support code and examples uniform and all working correctly....
Fernando Perez -
Show More
@@ -115,13 +115,14 b' class InputHookManager(object):'
115 115 elif self._apps.has_key(gui):
116 116 del self._apps[gui]
117 117
118 def enable_wx(self):
118 def enable_wx(self, app=None):
119 119 """Enable event loop integration with wxPython.
120 120
121 121 Parameters
122 122 ----------
123 app : bool
124 Create a running application object or not.
123 app : WX Application, optional.
124 Running application to use. If not given, we probe WX for an
125 existing application object, and create a new one if none is found.
125 126
126 127 Notes
127 128 -----
@@ -129,22 +130,19 b' class InputHookManager(object):'
129 130 the wxPython to integrate with terminal based applications like
130 131 IPython.
131 132
132 If ``app`` is True, we create an :class:`wx.App` as follows::
133 If ``app`` is not given we probe for an existing one, and return it if
134 found. If no existing app is found, we create an :class:`wx.App` as
135 follows::
133 136
134 137 import wx
135 138 app = wx.App(redirect=False, clearSigInt=False)
136
137 Both options this constructor are important for things to work
138 properly in an interactive context.
139
140 But, we first check to see if an application has already been
141 created. If so, we simply return that instance.
142 139 """
143 140 from IPython.lib.inputhookwx import inputhook_wx
144 141 self.set_inputhook(inputhook_wx)
145 142 self._current_gui = GUI_WX
146 143 import wx
147 app = wx.GetApp()
144 if app is None:
145 app = wx.GetApp()
148 146 if app is None:
149 147 app = wx.App(redirect=False, clearSigInt=False)
150 148 app._in_event_loop = True
@@ -160,13 +158,14 b' class InputHookManager(object):'
160 158 self._apps[GUI_WX]._in_event_loop = False
161 159 self.clear_inputhook()
162 160
163 def enable_qt4(self):
161 def enable_qt4(self, app=None):
164 162 """Enable event loop integration with PyQt4.
165 163
166 164 Parameters
167 165 ----------
168 app : bool
169 Create a running application object or not.
166 app : Qt Application, optional.
167 Running application to use. If not given, we probe Qt for an
168 existing application object, and create a new one if none is found.
170 169
171 170 Notes
172 171 -----
@@ -174,13 +173,12 b' class InputHookManager(object):'
174 173 the PyQt4 to integrate with terminal based applications like
175 174 IPython.
176 175
177 If ``app`` is True, we create an :class:`QApplication` as follows::
176 If ``app`` is not given we probe for an existing one, and return it if
177 found. If no existing app is found, we create an :class:`QApplication`
178 as follows::
178 179
179 180 from PyQt4 import QtCore
180 181 app = QtGui.QApplication(sys.argv)
181
182 But, we first check to see if an application has already been
183 created. If so, we simply return that instance.
184 182 """
185 183 from IPython.external.qt_for_kernel import QtCore, QtGui
186 184
@@ -205,7 +203,8 b' class InputHookManager(object):'
205 203 pass
206 204
207 205 self._current_gui = GUI_QT4
208 app = QtCore.QCoreApplication.instance()
206 if app is None:
207 app = QtCore.QCoreApplication.instance()
209 208 if app is None:
210 209 app = QtGui.QApplication([" "])
211 210 app._in_event_loop = True
@@ -221,14 +220,15 b' class InputHookManager(object):'
221 220 self._apps[GUI_QT4]._in_event_loop = False
222 221 self.clear_inputhook()
223 222
224 def enable_gtk(self, app=False):
223 def enable_gtk(self, app=None):
225 224 """Enable event loop integration with PyGTK.
226 225
227 226 Parameters
228 227 ----------
229 app : bool
230 Create a running application object or not. Because gtk does't
231 have an app class, this does nothing.
228 app : ignored
229 Ignored, it's only a placeholder to keep the call signature of all
230 gui activation methods consistent, which simplifies the logic of
231 supporting magics.
232 232
233 233 Notes
234 234 -----
@@ -253,21 +253,24 b' class InputHookManager(object):'
253 253 """
254 254 self.clear_inputhook()
255 255
256 def enable_tk(self, app=False):
256 def enable_tk(self, app=None):
257 257 """Enable event loop integration with Tk.
258 258
259 259 Parameters
260 260 ----------
261 app : bool
262 Create a running application object or not.
261 app : toplevel :class:`Tkinter.Tk` widget, optional.
262 Running application to use. If not given, we probe Qt for an
263 existing application object, and create a new one if none is found.
263 264
264 265 Notes
265 266 -----
266 Currently this is a no-op as creating a :class:`Tkinter.Tk` object
267 If you have already created a :class:`Tkinter.Tk` object, the only
268 thing done by this method is to register with the
269 :class:`InputHookManager`, since creating that object automatically
267 270 sets ``PyOS_InputHook``.
268 271 """
269 272 self._current_gui = GUI_TK
270 if app:
273 if app is None:
271 274 import Tkinter
272 275 app = Tkinter.Tk()
273 276 app.withdraw()
@@ -302,7 +305,7 b' clear_app_refs = inputhook_manager.clear_app_refs'
302 305
303 306
304 307 # Convenience function to switch amongst them
305 def enable_gui(gui=None):
308 def enable_gui(gui=None, app=None):
306 309 """Switch amongst GUI input hooks by name.
307 310
308 311 This is just a utility wrapper around the methods of the InputHookManager
@@ -314,8 +317,11 b' def enable_gui(gui=None):'
314 317 If None, clears input hook, otherwise it must be one of the recognized
315 318 GUI names (see ``GUI_*`` constants in module).
316 319
317 app : optional, bool
318 If true, create an app object and return it.
320 app : optional, existing application object.
321 For toolkits that have the concept of a global app, you can supply an
322 existing one. If not given, the toolkit will be probed for one, and if
323 none is found, a new one will be created. Note that GTK does not have
324 this concept, and passing an app if `gui`=="GTK" will raise an error.
319 325
320 326 Returns
321 327 -------
@@ -333,7 +339,7 b' def enable_gui(gui=None):'
333 339 try:
334 340 gui_hook = guis[gui]
335 341 except KeyError:
336 e="Invalid GUI request %r, valid ones are:%s" % (gui, guis.keys())
342 e = "Invalid GUI request %r, valid ones are:%s" % (gui, guis.keys())
337 343 raise ValueError(e)
338 return gui_hook()
344 return gui_hook(app)
339 345
@@ -38,6 +38,3 b' try:'
38 38 enable_gtk()
39 39 except ImportError:
40 40 gtk.main()
41
42
43
@@ -35,7 +35,15 b" if __name__ == '__main__':"
35 35 sw.show()
36 36
37 37 try:
38 from IPython.lib.inputhook import enable_qt4
39 enable_qt4()
38 # Note: the following form allows this script to work both inside
39 # ipython and without it, but `%gui qt` MUST be run first (or
40 # equivalently, ipython could have been started with `--gui=qt`).
41 from IPython.lib.guisupport import start_event_loop_qt4
42 start_event_loop_qt4(app)
43
44 # This from doesn't require the gui support to have been enabled in
45 # advance, but it won't work if the script is run as a standalone app
46 # outside of IPython while the user does have IPython available.
47 #from IPython.lib.inputhook import enable_qt4; enable_qt4(app)
40 48 except ImportError:
41 49 app.exec_()
@@ -27,6 +27,6 b' root = Tk()'
27 27 app = MyApp(root)
28 28
29 29 try:
30 from IPython import enable_tk; enable_tk(root)
30 from IPython.lib.inputhook import enable_tk; enable_tk(root)
31 31 except ImportError:
32 32 root.mainloop()
@@ -1,3 +1,4 b''
1 #!/usr/bin/env python
1 2 """A Simple wx example to test IPython's event loop integration.
2 3
3 4 To run this do:
@@ -97,13 +98,14 b' class MyApp(wx.App):'
97 98 frame.Show(True)
98 99 return True
99 100
100 app = wx.GetApp()
101 if app is None:
102 app = MyApp(redirect=False, clearSigInt=False)
101 if __name__ == '__main__':
102 app = wx.GetApp()
103 if app is None:
104 app = MyApp(redirect=False, clearSigInt=False)
103 105
104 try:
105 from IPython.lib.inputhook import enable_wx
106 enable_wx(app)
107 except ImportError:
108 app.MainLoop()
106 try:
107 from IPython.lib.inputhook import enable_wx
108 enable_wx(app)
109 except ImportError:
110 app.MainLoop()
109 111
General Comments 0
You need to be logged in to leave comments. Login now