##// END OF EJS Templates
Merge pull request #11295 from meeseeksmachine/auto-backport-of-pr-10974-on-5.x...
Merge pull request #11295 from meeseeksmachine/auto-backport-of-pr-10974-on-5.x Backport PR #10974 on branch 5.x (IPython breaks import matplotlib.pyplot when there is no display)

File last commit:

r24548:9c898df3
r24552:2b56bcb6 merge
Show More
qt.py
49 lines | 1.9 KiB | text/x-python | PythonLexer
Thomas Kluyver
Try to fix Qt event loop, take III...
r22654 import sys
Matthias Bussonnier
Backport PR #10974: IPython breaks import matplotlib.pyplot when there is no display
r24548 import os
Thomas Kluyver
Write & borrow some inputhooks for prompt_toolkit
r21934 from IPython.external.qt_for_kernel import QtCore, QtGui
Thomas Kluyver
Create a QApplication for inputhook if one doesn't already exist...
r22707 # If we create a QApplication, keep a reference to it so that it doesn't get
# garbage collected.
_appref = None
Matthias Bussonnier
Backport PR #10974: IPython breaks import matplotlib.pyplot when there is no display
r24548 _already_warned = False
Matthias Bussonnier
Backport PR #10301: FIX: re-order qt eventloop hook a bit...
r23382
Thomas Kluyver
Write & borrow some inputhooks for prompt_toolkit
r21934 def inputhook(context):
Thomas Kluyver
Create a QApplication for inputhook if one doesn't already exist...
r22707 global _appref
Thomas Kluyver
Write & borrow some inputhooks for prompt_toolkit
r21934 app = QtCore.QCoreApplication.instance()
if not app:
Matthias Bussonnier
Backport PR #10974: IPython breaks import matplotlib.pyplot when there is no display
r24548 if sys.platform == 'linux':
if not os.environ.get('DISPLAY') \
and not os.environ.get('WAYLAND_DISPLAY'):
import warnings
global _already_warned
if not _already_warned:
_already_warned = True
warnings.warn(
'The DISPLAY or WAYLAND_DISPLAY enviroment variable is '
'not set or empty and Qt5 requires this enviroment '
'variable. Deactivate Qt5 code.'
)
return
Thomas Kluyver
Create a QApplication for inputhook if one doesn't already exist...
r22707 _appref = app = QtGui.QApplication([" "])
Thomas Kluyver
Write & borrow some inputhooks for prompt_toolkit
r21934 event_loop = QtCore.QEventLoop(app)
Thomas Kluyver
Try to fix Qt event loop, take III...
r22654
if sys.platform == 'win32':
# The QSocketNotifier method doesn't appear to work on Windows.
# Use polling instead.
timer = QtCore.QTimer()
timer.timeout.connect(event_loop.quit)
while not context.input_is_ready():
timer.start(50) # 50 ms
event_loop.exec_()
timer.stop()
else:
# On POSIX platforms, we can use a file descriptor to quit the event
# loop when there is input ready to read.
Matthias Bussonnier
Backport PR #10301: FIX: re-order qt eventloop hook a bit...
r23382 notifier = QtCore.QSocketNotifier(context.fileno(),
QtCore.QSocketNotifier.Read)
# connect the callback we care about before we turn it on
Thomas Kluyver
Try to fix Qt event loop, take III...
r22654 notifier.activated.connect(event_loop.exit)
Matthias Bussonnier
Backport PR #10301: FIX: re-order qt eventloop hook a bit...
r23382 notifier.setEnabled(True)
# only start the event loop we are not already flipped
if not context.input_is_ready():
event_loop.exec_()