Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a7a148

Browse files
committed
MNT: more minor tweaks to qt_compat.py
If we the backend is not a Qt* backend, a version of pyqt has not been imported and PyQt5 is not installed qt_compat would raise as ImportError. Now it will gracefully fall back to PyQt4.
1 parent 87c9e4d commit 9a7a148

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

lib/matplotlib/backends/qt_compat.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,18 @@
109109
except:
110110
res = 'QVariant API v2 specification failed. Defaulting to v1.'
111111
verbose.report(cond + res, 'helpful')
112+
if QT_API == QT_API_PYQT5:
113+
try:
114+
from PyQt5 import QtCore, QtGui, QtWidgets
115+
_getSaveFileName = QtWidgets.QFileDialog.getSaveFileName
116+
except ImportError:
117+
# fell through, tried PyQt5, failed fall back to PyQt4
118+
QT_API = rcParams['backend.qt4']
119+
QT_RC_MAJOR_VERSION = 4
112120

121+
# needs to be if so we can re-test the value of QT_API which may
122+
# have been changed in the above if block
113123
if QT_API in [QT_API_PYQT, QT_API_PYQTv2]: # PyQt4 API
114-
115124
from PyQt4 import QtCore, QtGui
116125

117126
try:
@@ -131,9 +140,8 @@ def _getSaveFileName(*args, **kwargs):
131140
def _getSaveFileName(*args, **kwargs):
132141
return QtGui.QFileDialog.getSaveFileName(*args, **kwargs), None
133142

134-
else: # PyQt5 API
135-
from PyQt5 import QtCore, QtGui, QtWidgets
136-
_getSaveFileName = QtWidgets.QFileDialog.getSaveFileName
143+
else:
144+
raise RuntimeError("No PyQt bindings found despite sip importing")
137145

138146
# Alias PyQt-specific functions for PySide compatibility.
139147
QtCore.Signal = QtCore.pyqtSignal

0 commit comments

Comments
 (0)