Show More
@@ -1,49 +1,53 b'' | |||
|
1 | 1 | import sys |
|
2 | 2 | import os |
|
3 | 3 | from IPython.external.qt_for_kernel import QtCore, QtGui |
|
4 | 4 | |
|
5 | 5 | # If we create a QApplication, keep a reference to it so that it doesn't get |
|
6 | 6 | # garbage collected. |
|
7 | 7 | _appref = None |
|
8 | 8 | _already_warned = False |
|
9 | 9 | |
|
10 | ||
|
10 | 11 | def inputhook(context): |
|
11 | 12 | global _appref |
|
12 | 13 | app = QtCore.QCoreApplication.instance() |
|
13 | 14 | if not app: |
|
14 | 15 | if sys.platform == 'linux': |
|
15 | 16 | if not os.environ.get('DISPLAY') \ |
|
16 | 17 | and not os.environ.get('WAYLAND_DISPLAY'): |
|
17 | 18 | import warnings |
|
18 | 19 | global _already_warned |
|
19 | 20 | if not _already_warned: |
|
20 | 21 | _already_warned = True |
|
21 | 22 | warnings.warn( |
|
22 | 23 | 'The DISPLAY or WAYLAND_DISPLAY environment variable is ' |
|
23 | 24 | 'not set or empty and Qt5 requires this environment ' |
|
24 | 25 | 'variable. Deactivate Qt5 code.' |
|
25 | 26 | ) |
|
26 | 27 | return |
|
27 | 28 | _appref = app = QtGui.QApplication([" "]) |
|
28 | 29 | event_loop = QtCore.QEventLoop(app) |
|
29 | 30 | |
|
30 | 31 | if sys.platform == 'win32': |
|
31 | 32 | # The QSocketNotifier method doesn't appear to work on Windows. |
|
32 | 33 | # Use polling instead. |
|
33 | 34 | timer = QtCore.QTimer() |
|
34 | 35 | timer.timeout.connect(event_loop.quit) |
|
35 | 36 | while not context.input_is_ready(): |
|
36 | 37 | timer.start(50) # 50 ms |
|
37 | 38 | event_loop.exec_() |
|
38 | 39 | timer.stop() |
|
39 | 40 | else: |
|
40 | 41 | # On POSIX platforms, we can use a file descriptor to quit the event |
|
41 | 42 | # loop when there is input ready to read. |
|
42 | 43 | notifier = QtCore.QSocketNotifier(context.fileno(), |
|
43 | 44 | QtCore.QSocketNotifier.Read) |
|
44 | # connect the callback we care about before we turn it on | |
|
45 | notifier.activated.connect(event_loop.exit) | |
|
46 | notifier.setEnabled(True) | |
|
47 | # only start the event loop we are not already flipped | |
|
48 | if not context.input_is_ready(): | |
|
49 | event_loop.exec_() | |
|
45 | try: | |
|
46 | # connect the callback we care about before we turn it on | |
|
47 | notifier.activated.connect(event_loop.exit) | |
|
48 | notifier.setEnabled(True) | |
|
49 | # only start the event loop we are not already flipped | |
|
50 | if not context.input_is_ready(): | |
|
51 | event_loop.exec_() | |
|
52 | finally: | |
|
53 | notifier.setEnabled(False) |
General Comments 0
You need to be logged in to leave comments.
Login now