##// 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 elif self._apps.has_key(gui):
115 elif self._apps.has_key(gui):
116 del self._apps[gui]
116 del self._apps[gui]
117
117
118 def enable_wx(self):
118 def enable_wx(self, app=None):
119 """Enable event loop integration with wxPython.
119 """Enable event loop integration with wxPython.
120
120
121 Parameters
121 Parameters
122 ----------
122 ----------
123 app : bool
123 app : WX Application, optional.
124 Create a running application object or not.
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 Notes
127 Notes
127 -----
128 -----
@@ -129,22 +130,19 b' class InputHookManager(object):'
129 the wxPython to integrate with terminal based applications like
130 the wxPython to integrate with terminal based applications like
130 IPython.
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 import wx
137 import wx
135 app = wx.App(redirect=False, clearSigInt=False)
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 from IPython.lib.inputhookwx import inputhook_wx
140 from IPython.lib.inputhookwx import inputhook_wx
144 self.set_inputhook(inputhook_wx)
141 self.set_inputhook(inputhook_wx)
145 self._current_gui = GUI_WX
142 self._current_gui = GUI_WX
146 import wx
143 import wx
147 app = wx.GetApp()
144 if app is None:
145 app = wx.GetApp()
148 if app is None:
146 if app is None:
149 app = wx.App(redirect=False, clearSigInt=False)
147 app = wx.App(redirect=False, clearSigInt=False)
150 app._in_event_loop = True
148 app._in_event_loop = True
@@ -160,13 +158,14 b' class InputHookManager(object):'
160 self._apps[GUI_WX]._in_event_loop = False
158 self._apps[GUI_WX]._in_event_loop = False
161 self.clear_inputhook()
159 self.clear_inputhook()
162
160
163 def enable_qt4(self):
161 def enable_qt4(self, app=None):
164 """Enable event loop integration with PyQt4.
162 """Enable event loop integration with PyQt4.
165
163
166 Parameters
164 Parameters
167 ----------
165 ----------
168 app : bool
166 app : Qt Application, optional.
169 Create a running application object or not.
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 Notes
170 Notes
172 -----
171 -----
@@ -174,13 +173,12 b' class InputHookManager(object):'
174 the PyQt4 to integrate with terminal based applications like
173 the PyQt4 to integrate with terminal based applications like
175 IPython.
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 from PyQt4 import QtCore
180 from PyQt4 import QtCore
180 app = QtGui.QApplication(sys.argv)
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 from IPython.external.qt_for_kernel import QtCore, QtGui
183 from IPython.external.qt_for_kernel import QtCore, QtGui
186
184
@@ -205,7 +203,8 b' class InputHookManager(object):'
205 pass
203 pass
206
204
207 self._current_gui = GUI_QT4
205 self._current_gui = GUI_QT4
208 app = QtCore.QCoreApplication.instance()
206 if app is None:
207 app = QtCore.QCoreApplication.instance()
209 if app is None:
208 if app is None:
210 app = QtGui.QApplication([" "])
209 app = QtGui.QApplication([" "])
211 app._in_event_loop = True
210 app._in_event_loop = True
@@ -221,14 +220,15 b' class InputHookManager(object):'
221 self._apps[GUI_QT4]._in_event_loop = False
220 self._apps[GUI_QT4]._in_event_loop = False
222 self.clear_inputhook()
221 self.clear_inputhook()
223
222
224 def enable_gtk(self, app=False):
223 def enable_gtk(self, app=None):
225 """Enable event loop integration with PyGTK.
224 """Enable event loop integration with PyGTK.
226
225
227 Parameters
226 Parameters
228 ----------
227 ----------
229 app : bool
228 app : ignored
230 Create a running application object or not. Because gtk does't
229 Ignored, it's only a placeholder to keep the call signature of all
231 have an app class, this does nothing.
230 gui activation methods consistent, which simplifies the logic of
231 supporting magics.
232
232
233 Notes
233 Notes
234 -----
234 -----
@@ -253,21 +253,24 b' class InputHookManager(object):'
253 """
253 """
254 self.clear_inputhook()
254 self.clear_inputhook()
255
255
256 def enable_tk(self, app=False):
256 def enable_tk(self, app=None):
257 """Enable event loop integration with Tk.
257 """Enable event loop integration with Tk.
258
258
259 Parameters
259 Parameters
260 ----------
260 ----------
261 app : bool
261 app : toplevel :class:`Tkinter.Tk` widget, optional.
262 Create a running application object or not.
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 Notes
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 sets ``PyOS_InputHook``.
270 sets ``PyOS_InputHook``.
268 """
271 """
269 self._current_gui = GUI_TK
272 self._current_gui = GUI_TK
270 if app:
273 if app is None:
271 import Tkinter
274 import Tkinter
272 app = Tkinter.Tk()
275 app = Tkinter.Tk()
273 app.withdraw()
276 app.withdraw()
@@ -302,7 +305,7 b' clear_app_refs = inputhook_manager.clear_app_refs'
302
305
303
306
304 # Convenience function to switch amongst them
307 # Convenience function to switch amongst them
305 def enable_gui(gui=None):
308 def enable_gui(gui=None, app=None):
306 """Switch amongst GUI input hooks by name.
309 """Switch amongst GUI input hooks by name.
307
310
308 This is just a utility wrapper around the methods of the InputHookManager
311 This is just a utility wrapper around the methods of the InputHookManager
@@ -314,8 +317,11 b' def enable_gui(gui=None):'
314 If None, clears input hook, otherwise it must be one of the recognized
317 If None, clears input hook, otherwise it must be one of the recognized
315 GUI names (see ``GUI_*`` constants in module).
318 GUI names (see ``GUI_*`` constants in module).
316
319
317 app : optional, bool
320 app : optional, existing application object.
318 If true, create an app object and return it.
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 Returns
326 Returns
321 -------
327 -------
@@ -333,7 +339,7 b' def enable_gui(gui=None):'
333 try:
339 try:
334 gui_hook = guis[gui]
340 gui_hook = guis[gui]
335 except KeyError:
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 raise ValueError(e)
343 raise ValueError(e)
338 return gui_hook()
344 return gui_hook(app)
339
345
@@ -38,6 +38,3 b' try:'
38 enable_gtk()
38 enable_gtk()
39 except ImportError:
39 except ImportError:
40 gtk.main()
40 gtk.main()
41
42
43
@@ -35,7 +35,15 b" if __name__ == '__main__':"
35 sw.show()
35 sw.show()
36
36
37 try:
37 try:
38 from IPython.lib.inputhook import enable_qt4
38 # Note: the following form allows this script to work both inside
39 enable_qt4()
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 except ImportError:
48 except ImportError:
41 app.exec_()
49 app.exec_()
@@ -27,6 +27,6 b' root = Tk()'
27 app = MyApp(root)
27 app = MyApp(root)
28
28
29 try:
29 try:
30 from IPython import enable_tk; enable_tk(root)
30 from IPython.lib.inputhook import enable_tk; enable_tk(root)
31 except ImportError:
31 except ImportError:
32 root.mainloop()
32 root.mainloop()
@@ -1,3 +1,4 b''
1 #!/usr/bin/env python
1 """A Simple wx example to test IPython's event loop integration.
2 """A Simple wx example to test IPython's event loop integration.
2
3
3 To run this do:
4 To run this do:
@@ -97,13 +98,14 b' class MyApp(wx.App):'
97 frame.Show(True)
98 frame.Show(True)
98 return True
99 return True
99
100
100 app = wx.GetApp()
101 if __name__ == '__main__':
101 if app is None:
102 app = wx.GetApp()
102 app = MyApp(redirect=False, clearSigInt=False)
103 if app is None:
104 app = MyApp(redirect=False, clearSigInt=False)
103
105
104 try:
106 try:
105 from IPython.lib.inputhook import enable_wx
107 from IPython.lib.inputhook import enable_wx
106 enable_wx(app)
108 enable_wx(app)
107 except ImportError:
109 except ImportError:
108 app.MainLoop()
110 app.MainLoop()
109
111
General Comments 0
You need to be logged in to leave comments. Login now