diff --git a/IPython/core/ipmaker.py b/IPython/core/ipmaker.py index ee879e6..d279958 100644 --- a/IPython/core/ipmaker.py +++ b/IPython/core/ipmaker.py @@ -42,6 +42,7 @@ import __builtin__ import os import sys from pprint import pprint +import warnings # Our own from IPython.utils import DPyGetOpt @@ -53,13 +54,24 @@ from IPython.core.iplib import InteractiveShell from IPython.core.usage import cmd_line_usage, interactive_usage from IPython.utils.genutils import * + def force_import(modname,force_reload=False): if modname in sys.modules and force_reload: info("reloading: %s" % modname) reload(sys.modules[modname]) else: __import__(modname) - + + +def threaded_shell_warning(): + msg = """ + +The IPython threaded shells and their associated command line +arguments (pylab/wthread/gthread/qthread/q4thread) have been +deprecated. See the %gui magic for information on the new interface. +""" + warnings.warn(msg, category=DeprecationWarning, stacklevel=1) + #----------------------------------------------------------------------------- def make_IPython(argv=None,user_ns=None,user_global_ns=None,debug=1, @@ -330,6 +342,13 @@ object? -> Details about 'object'. ?object also works, ?? prints more. IP.magic_magic('-latex') sys.exit() + # Display the deprecation warnings about threaded shells + if opts_all.pylab == 1: threaded_shell_warning() + if opts_all.wthread == 1: threaded_shell_warning() + if opts_all.qthread == 1: threaded_shell_warning() + if opts_all.q4thread == 1: threaded_shell_warning() + if opts_all.gthread == 1: threaded_shell_warning() + # add personal ipythondir to sys.path so that users can put things in # there for customization sys.path.append(os.path.abspath(opts_all.ipythondir)) diff --git a/IPython/core/magic.py b/IPython/core/magic.py index 122e865..843954a 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -3524,4 +3524,47 @@ Defaulting color scheme to 'NoColor'""" print 'Doctest mode is:', print ['OFF','ON'][dstore.mode] + def magic_gui(self, parameter_s=''): + """Enable or disable IPython GUI event loop integration. + + This magic replaces IPython's threaded shells that were activated + using the (pylab/wthread/etc.) command line flags. GUI toolkits + can now be enabled, disabled and swtiched at runtime and keyboard + interrupts should work without any problems. The following toolkits + are supports: wxPython, PyQt4, PyGTK, and Tk:: + + %gui wx # enable wxPython event loop integration + %gui qt4 # enable PyQt4 event loop integration + %gui gtk # enable PyGTK event loop integration + %gui tk # enable Tk event loop integration + %gui # disable all event loop integration + + WARNING: after any of these has been called you can simply create + an application object, but DO NOT start the event loop yourself, as + we have already handled that. + + If you want us to create an appropriate application object add the + "-a" flag to your command:: + + %gui -a wx + + This is highly recommended for most users. + """ + from IPython.lib import inputhook + if "-a" in parameter_s: + app = True + else: + app = False + if not parameter_s: + inputhook.clear_inputhook() + elif 'wx' in parameter_s: + return inputhook.enable_wx(app) + elif 'qt4' in parameter_s: + return inputhook.enable_qt4(app) + elif 'gtk' in parameter_s: + return inputhook.enable_gtk(app) + elif 'tk' in parameter_s: + return inputhook.enable_tk(app) + + # end Magic diff --git a/IPython/lib/inputhook.py b/IPython/lib/inputhook.py index 6b0b1f5..b523cfd 100644 --- a/IPython/lib/inputhook.py +++ b/IPython/lib/inputhook.py @@ -16,6 +16,7 @@ Inputhook management for GUI event loop integration. #----------------------------------------------------------------------------- import ctypes +import sys #----------------------------------------------------------------------------- # Code @@ -65,7 +66,7 @@ class InputHookManager(object): self._reset() return original - def enable_wx(self): + def enable_wx(self, app=False): """Enable event loop integration with wxPython. This methods sets the PyOS_InputHook for wxPython, which allows @@ -85,6 +86,10 @@ class InputHookManager(object): """ from IPython.lib.inputhookwx import inputhook_wx self.set_inputhook(inputhook_wx) + if app: + import wx + app = wx.App(redirect=False, clearSigInt=False) + return app def disable_wx(self): """Disable event loop integration with wxPython. @@ -93,7 +98,7 @@ class InputHookManager(object): """ self.clear_inputhook() - def enable_qt4(self): + def enable_qt4(self, app=False): """Enable event loop integration with PyQt4. This methods sets the PyOS_InputHook for wxPython, which allows @@ -113,6 +118,10 @@ class InputHookManager(object): QtCore.pyqtRestoreInputHook() except AttributeError: pass + if app: + from PyQt4 import QtGui + app = QtGui.QApplication(sys.argv) + return app def disable_qt4(self): """Disable event loop integration with PyQt4. @@ -121,7 +130,7 @@ class InputHookManager(object): """ self.clear_inputhook() - def enable_gtk(self): + def enable_gtk(self, app=False): """Enable event loop integration with PyGTK. This methods sets the PyOS_InputHook for PyGTK, which allows @@ -147,7 +156,7 @@ class InputHookManager(object): """ self.clear_inputhook() - def enable_tk(self): + def enable_tk(self, app=False): # Creating a Tkinter.Tk object sets PyOS_InputHook() pass