From 51df12162743fd1251dd81d3f4533a462fa32166 2009-08-23 01:42:32 From: Fernando Perez Date: 2009-08-23 01:42:32 Subject: [PATCH] In-progress work on trying to get a robust inputhook setup. This is proving to be pretty hard and will require some changes to end-user code in the long run. It's simply impossible. --- diff --git a/IPython/lib/inputhook.py b/IPython/lib/inputhook.py index c97c822..2bfc953 100755 --- a/IPython/lib/inputhook.py +++ b/IPython/lib/inputhook.py @@ -22,6 +22,18 @@ import sys # Code #----------------------------------------------------------------------------- +def appstart_qt4(): + from PyQt4 import QtCore, QtGui + + app = QtCore.QCoreApplication.instance() + print 'qtapp:', app + if app is not None: + if current_gui() == 'qt4': + pass + else: + app.exec_() + + class _DummyMainloop(object): """A special manager to hijack GUI mainloops that is mostly a no-op. @@ -34,6 +46,12 @@ class _DummyMainloop(object): def __call__(self, *args, **kw): + force = kw.pop('force', False) + force = False + if force: + #print 'forced spin' # dbg + self.ml(*args, **kw) + if self.ihm.current_gui() == self.gui_type: pass else: @@ -45,14 +63,15 @@ def spin_qt4(): app = QtCore.QCoreApplication.instance() if (app is not None) and (app.thread() == QtCore.QThread.currentThread()): - timer = QtCore.QTimer() - QtCore.QObject.connect(timer, - QtCore.SIGNAL('timeout()'), - app, - QtCore.SLOT('quit()')) - timer.start(100) - QtCore.QCoreApplication.exec_() - timer.stop() + ## timer = QtCore.QTimer() + ## QtCore.QObject.connect(timer, + ## QtCore.SIGNAL('timeout()'), + ## app, + ## QtCore.SLOT('quit()')) + ## timer.start(100) + #QtCore.QCoreApplication.exec_(force=True) + QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents) + ##timer.stop() def spin_wx(): @@ -241,7 +260,7 @@ class InputHookManager(object): except AttributeError: pass self._current_gui = 'qt4' - self._hijack_qt4() + #self._hijack_qt4() if app: from PyQt4 import QtGui app = QtCore.QCoreApplication.instance() diff --git a/docs/examples/core/gui-qt.py b/docs/examples/core/gui-qt.py index 1a5d89a..ce8b870 100755 --- a/docs/examples/core/gui-qt.py +++ b/docs/examples/core/gui-qt.py @@ -34,4 +34,9 @@ if __name__ == '__main__': sw = SimpleWindow() sw.show() - app.exec_() + try: + import IPython.lib.inputhook as i; i.appstart_qt4() + except ImportError: + app.exec_() + + #import time; time.sleep(10) diff --git a/docs/examples/core/switchgui.py b/docs/examples/core/switchgui.py new file mode 100644 index 0000000..9037329 --- /dev/null +++ b/docs/examples/core/switchgui.py @@ -0,0 +1,57 @@ +"""Test the new %gui command. Run this in ipython as + +%run switchgui [backend] + +where the optional backend can be one of: qt4, gtk, tk, wx. +""" + +import sys +import time + +import IPython.core.ipapi as ipapi +ip = ipapi.get() + +from IPython.lib import inputhook + +try: + backend = sys.argv[1] + #a = ip.magic('gui -a %s' % backend) + #a = ip.magic('gui %s' % backend) +except IndexError: + backend = 'qt' + +backends = dict(wx='wxagg', qt='qt4agg', gtk='gtkagg', tk='tkagg') + +import matplotlib +matplotlib.use(backends[backend]) +#matplotlib.interactive(True) + +import matplotlib +from matplotlib import pyplot as plt, mlab, pylab +import numpy as np + +from numpy import * +from matplotlib.pyplot import * + +x = np.linspace(0,pi,100) + +print "A plot has been created" +line, = plot(x,sin(2*x)) +plt.show() +inputhook.spin_qt4() + +#raw_input("Press Enter to continue") + +print "I will now count until 10, please hit Ctrl-C before I'm done..." +print "IPython should stop counting and return to the prompt without crashing." +print +line_x = line.get_data()[0] +for i in range(1,51): + print i, + sys.stdout.flush() + line.set_data(line_x,sin(x*i)) + plt.title('i=%d' % i) + #plt.show() + plt.draw() + inputhook.spin_qt4() + #time.sleep(0.04)