Show More
@@ -2,74 +2,81 b'' | |||||
2 |
|
2 | |||
3 | This is the import used for the `gui=qt` or `pylab=qt` initialization. |
|
3 | This is the import used for the `gui=qt` or `pylab=qt` initialization. | |
4 |
|
4 | |||
5 | Priority: |
|
5 | Import Priority: | |
6 |
|
6 | |||
7 | if matplotlib has been imported: |
|
7 | if matplotlib has been imported and doesn't support v2 (<= 1.0.1): | |
8 | # get here with pylab=qt |
|
8 | use PyQt4 @v1 | |
9 | if matplotlib doesn't support v2 (<= 1.0.1): |
|
|||
10 | use PyQt4 @v1 |
|
|||
11 | else: |
|
|||
12 | ask matplotlib which Qt it's using |
|
|||
13 | if it said PyQt: |
|
|||
14 | use PyQt4 @v1 |
|
|||
15 | elif it said PySide: |
|
|||
16 | use PySide |
|
|||
17 |
|
9 | |||
18 | if matplotlib had nothing to say, or matplotlib not imported: |
|
10 | Next, ask ETS' QT_API env variable | |
19 | # get here with gui=qt, or if matplotlib didn't tell us anything |
|
11 | ||
20 | ask ETS' QT_API env variable |
|
12 | if QT_API not set: | |
|
13 | ask matplotlib via rcParams['backend.qt4'] | |||
|
14 | if it said PyQt: | |||
|
15 | use PyQt4 @v1 | |||
|
16 | elif it said PySide: | |||
|
17 | use PySide | |||
21 |
|
18 | |||
22 | if QT_API not set: |
|
19 | else: (matplotlib said nothing) | |
23 |
# this is the |
|
20 | # this is the default path - nobody told us anything | |
24 | try: |
|
21 | try: | |
25 | PyQt @v1 |
|
22 | PyQt @v1 | |
26 | except: |
|
23 | except: | |
27 | fallback on PySide |
|
24 | fallback on PySide | |
28 |
|
|
25 | else: | |
29 |
|
|
26 | use PyQt @v2 or PySide, depending on QT_API | |
30 |
|
|
27 | because ETS doesn't work with PyQt @v1. | |
|
28 | ||||
31 | """ |
|
29 | """ | |
32 |
|
30 | |||
33 | import os |
|
31 | import os | |
34 | import sys |
|
32 | import sys | |
35 |
|
33 | |||
36 | matplotlib = sys.modules.get('matplotlib') |
|
34 | matplotlib = sys.modules.get('matplotlib') | |
37 | if matplotlib: |
|
35 | if matplotlib and matplotlib.__version__ <= '1.0.1': | |
38 | # ask matplotlib first (get here with pylab=qt) |
|
36 | # 1.0.1 doesn't support pyside or v2, so stick with PyQt @v1, | |
39 | if matplotlib.__version__ <= '1.0.1': |
|
37 | # and ignore everything else | |
40 | # 1.0.1 doesn't support pyside or v2, so force PyQt @v1 |
|
38 | from PyQt4 import QtCore, QtGui | |
41 | mod = 'PyQt4' |
|
|||
42 | else: |
|
|||
43 | # this rc option has been proposed, but is yet not in matplotlib master |
|
|||
44 | # as of writing. |
|
|||
45 | mod = matplotlib.rcParams.get('backend.qt4', None) |
|
|||
46 | else: |
|
39 | else: | |
47 | # get here with `gui=qt` |
|
40 | # ask QT_API ETS variable *first* | |
48 | mod = None |
|
|||
49 |
|
||||
50 | if mod is None: |
|
|||
51 | # matplotlib not imported or had nothing to say. |
|
|||
52 | # ask QT_API ETS variable |
|
|||
53 | QT_API = os.environ.get('QT_API', None) |
|
41 | QT_API = os.environ.get('QT_API', None) | |
54 | if QT_API is None: |
|
42 | if QT_API is None: | |
55 | try: |
|
43 | # QT_API not set, ask matplotlib if it was imported (e.g. `pylab=qt`) | |
56 | # default to unconfigured PyQt4 |
|
44 | if matplotlib: | |
57 | from PyQt4 import QtCore, QtGui |
|
45 | mpqt = matplotlib.rcParams.get('backend.qt4', None) | |
58 | except ImportError: |
|
46 | else: | |
59 |
|
|
47 | mpqt = None | |
|
48 | if mpqt is None: | |||
|
49 | # matplotlib not imported or had nothing to say. | |||
60 | try: |
|
50 | try: | |
61 | from PySide import QtCore, QtGui |
|
51 | # default to unconfigured PyQt4 | |
|
52 | from PyQt4 import QtCore, QtGui | |||
62 | except ImportError: |
|
53 | except ImportError: | |
63 | raise ImportError('Cannot import PySide or PyQt4') |
|
54 | # fallback on PySide | |
|
55 | try: | |||
|
56 | from PySide import QtCore, QtGui | |||
|
57 | except ImportError: | |||
|
58 | raise ImportError('Cannot import PySide or PyQt4') | |||
|
59 | elif mpqt.lower() == 'pyqt4': | |||
|
60 | # import PyQt4 unconfigured | |||
|
61 | from PyQt4 import QtCore, QtGui | |||
|
62 | elif mpqt.lower() == 'pyside': | |||
|
63 | from PySide import QtCore, QtGui | |||
|
64 | else: | |||
|
65 | raise ImportError("unhandled value for backend.qt4 from matplotlib: %r"%mpqt) | |||
64 | else: |
|
66 | else: | |
65 | # QT_API specified, use PySide or PyQt+v2 API from external.qt |
|
67 | # QT_API specified, use PySide or PyQt+v2 API from external.qt | |
66 | # this means ETS is likely to be used, which requires v2 |
|
68 | # this means ETS is likely to be used, which requires v2 | |
67 | from IPython.external.qt import QtCore, QtGui |
|
69 | try: | |
|
70 | from IPython.external.qt import QtCore, QtGui | |||
|
71 | except ValueError as e: | |||
|
72 | if 'API' in str(e): | |||
|
73 | # API mismatch, give more meaningful message | |||
|
74 | raise ImportError(""" | |||
|
75 | Assigning the ETS variable `QT_API=pyqt` implies PyQt's v2 API for | |||
|
76 | QString and QVariant, but PyQt has already been imported | |||
|
77 | with v1 APIs. You must unset QT_API to work with PyQt4 | |||
|
78 | in its default mode. | |||
|
79 | """) | |||
|
80 | else: | |||
|
81 | raise | |||
68 |
|
82 | |||
69 | elif mod.lower() == 'pyqt4': |
|
|||
70 | # import PyQt4 unconfigured |
|
|||
71 | from PyQt4 import QtCore, QtGui |
|
|||
72 | elif mod.lower() == 'pyside': |
|
|||
73 | from PySide import QtCore, QtGui |
|
|||
74 | else: |
|
|||
75 | raise ImportError("unhandled value for backend.qt4 from matplotlib: %r"%mod) No newline at end of file |
|
@@ -1269,29 +1269,29 b" v2 is also the default for PyQt4 on Python 3. IPython's code for the QtConsole" | |||||
1269 | uses v2, but you can still use any interface in your code, since the |
|
1269 | uses v2, but you can still use any interface in your code, since the | |
1270 | Qt frontend is in a different process. |
|
1270 | Qt frontend is in a different process. | |
1271 |
|
1271 | |||
1272 | If you launch IPython in pylab mode with ``ipython pylab=qt``, then IPython |
|
1272 | The default will be to import PyQt4 without configuration of the APIs, thus | |
1273 | will ask matplotlib which Qt library to use, via the 'backend.qt4' rcParam. |
|
1273 | matching what most applications would expect. It will fall back of PySide if | |
1274 | If matplotlib is version 1.0.1 or older, then IPython will always use PyQt4 |
|
1274 | PyQt4 is unavailable. | |
1275 | without setting the v2 APIs. |
|
|||
1276 |
|
||||
1277 | If you just integrate the Qt event loop with ``ipython gui=qt``, then IPython |
|
|||
1278 | has a few more possibilities. The default will be to import PyQt4 without |
|
|||
1279 | configuration of the APIs, thus matching what most applications would expect. |
|
|||
1280 | It will fall back of PySide if PyQt4 is unavailable. |
|
|||
1281 |
|
1275 | |||
1282 | If specified, IPython will respect the environment variable ``QT_API`` used |
|
1276 | If specified, IPython will respect the environment variable ``QT_API`` used | |
1283 | by ETS. ETS 4.0 also works with both PyQt4 and PySide, but it requires |
|
1277 | by ETS. ETS 4.0 also works with both PyQt4 and PySide, but it requires | |
1284 |
PyQt4 to use |
|
1278 | PyQt4 to use its v2 API. So if ``QT_API=pyside`` PySide will be used, | |
1285 |
and if ``QT_API=pyqt`` then PyQt4 will be used with the v2 API for |
|
1279 | and if ``QT_API=pyqt`` then PyQt4 will be used *with the v2 API* for | |
1286 | QString and QVariant, so ETS codes like MayaVi will also work with IPython. |
|
1280 | QString and QVariant, so ETS codes like MayaVi will also work with IPython. | |
1287 |
|
1281 | |||
|
1282 | If you launch IPython in pylab mode with ``ipython pylab=qt``, then IPython | |||
|
1283 | will ask matplotlib which Qt library to use (only if QT_API is *not set*), | |||
|
1284 | via the 'backend.qt4' rcParam. | |||
|
1285 | If matplotlib is version 1.0.1 or older, then IPython will always use PyQt4 | |||
|
1286 | without setting the v2 APIs, since neither v2 PyQt nor PySide work. | |||
|
1287 | ||||
1288 | .. warning:: |
|
1288 | .. warning:: | |
1289 |
|
1289 | |||
1290 | Note that this means for ETS to work with PyQt4, ``QT_API`` *must* be set to |
|
1290 | Note that this means for ETS 4 to work with PyQt4, ``QT_API`` *must* be set to | |
1291 | work with IPython's qt integration, because otherwise PyQt4 will be loaded in |
|
1291 | work with IPython's qt integration, because otherwise PyQt4 will be loaded in | |
1292 | an incompatible mode. |
|
1292 | an incompatible mode. | |
1293 |
|
1293 | |||
1294 | It also means that you must not have ``QT_API`` set if you want to |
|
1294 | It also means that you must *not* have ``QT_API`` set if you want to | |
1295 | use ``gui=qt`` with code that requires PyQt4 API v1. |
|
1295 | use ``gui=qt`` with code that requires PyQt4 API v1. | |
1296 |
|
1296 | |||
1297 |
|
1297 |
General Comments 0
You need to be logged in to leave comments.
Login now