qt.py
30 lines
| 1.0 KiB
| text/x-python
|
PythonLexer
Thomas Kluyver
|
r22654 | import sys | ||
Thomas Kluyver
|
r21934 | from IPython.external.qt_for_kernel import QtCore, QtGui | ||
Thomas Kluyver
|
r22707 | # If we create a QApplication, keep a reference to it so that it doesn't get | ||
# garbage collected. | ||||
_appref = None | ||||
Thomas Kluyver
|
r21934 | def inputhook(context): | ||
Thomas Kluyver
|
r22707 | global _appref | ||
Thomas Kluyver
|
r21934 | app = QtCore.QCoreApplication.instance() | ||
if not app: | ||||
Thomas Kluyver
|
r22707 | _appref = app = QtGui.QApplication([" "]) | ||
Thomas Kluyver
|
r21934 | event_loop = QtCore.QEventLoop(app) | ||
Thomas Kluyver
|
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. | ||||
notifier = QtCore.QSocketNotifier(context.fileno(), QtCore.QSocketNotifier.Read) | ||||
notifier.setEnabled(True) | ||||
notifier.activated.connect(event_loop.exit) | ||||
event_loop.exec_() | ||||