Show More
@@ -139,65 +139,64 def inputhook_wx3(context): | |||||
139 |
|
139 | |||
140 |
|
140 | |||
141 | def inputhook_wxphoenix(context): |
|
141 | def inputhook_wxphoenix(context): | |
142 |
"""Run the wx event loop |
|
142 | """Run the wx event loop until the user provides more input. | |
143 |
|
143 | |||
144 | This is equivalent to inputhook_wx3, but has been updated to work with |
|
144 | This function uses the same approach to that used in | |
145 | wxPython Phoenix. |
|
145 | ipykernel.eventloops.loop_wx. | |
146 | """ |
|
146 | """ | |
147 |
|
147 | |||
148 | # See inputhook_wx3 for in-line comments. |
|
148 | app = wx.GetApp() | |
149 | try: |
|
149 | ||
150 | app = wx.GetApp() |
|
150 | if app is None: | |
151 | if app is not None: |
|
151 | return | |
152 | assert wx.IsMainThread() |
|
152 | ||
153 |
|
153 | # Wx uses milliseconds | ||
154 | if not callable(signal.getsignal(signal.SIGINT)): |
|
154 | poll_interval = 100 | |
155 | signal.signal(signal.SIGINT, signal.default_int_handler) |
|
155 | ||
156 |
|
156 | # This function gets polled periodically; when | ||
157 | evtloop = wx.GUIEventLoop() |
|
157 | # input is ready, the wx main loop is stopped. | |
158 | ea = wx.EventLoopActivator(evtloop) |
|
158 | def wake(): | |
159 | t = clock() |
|
159 | if context.input_is_ready(): | |
160 | while not context.input_is_ready(): |
|
160 | app.ExitMainLoop() | |
161 | while evtloop.Pending(): |
|
161 | ||
162 | t = clock() |
|
162 | # We have to put the wx.Timer in a wx.Frame for it to fire properly. | |
163 | evtloop.Dispatch() |
|
163 | # We make the Frame hidden when we create it in the main app below. | |
164 |
|
164 | class TimerFrame(wx.Frame): | ||
165 | # Not all events will be procesed by Dispatch - |
|
165 | def __init__(self, func): | |
166 | # we have to call ProcessPendingEvents as well |
|
166 | wx.Frame.__init__(self, None, -1) | |
167 | # to ensure that all events get processed. |
|
167 | self.timer = wx.Timer(self) | |
168 | app.ProcessPendingEvents() |
|
168 | self.timer.Start(poll_interval) | |
169 | evtloop.ProcessIdle() |
|
169 | self.Bind(wx.EVT_TIMER, self.on_timer) | |
170 |
|
170 | self.func = func | ||
171 | used_time = clock() - t |
|
171 | ||
172 |
|
172 | def on_timer(self, event): | ||
173 | if used_time > 10.0: |
|
173 | self.func() | |
174 | time.sleep(1.0) |
|
174 | ||
175 | elif used_time > 0.1: |
|
175 | frame = TimerFrame(wake) | |
176 | time.sleep(0.05) |
|
176 | frame.Show(False) | |
177 | else: |
|
177 | ||
178 | time.sleep(0.001) |
|
178 | # The import of wx on Linux sets the handler for signal.SIGINT | |
179 | del ea |
|
179 | # to 0. This is a bug in wx or gtk. We fix by just setting it | |
180 | except KeyboardInterrupt: |
|
180 | # back to the Python default. | |
181 | pass |
|
181 | if not callable(signal.getsignal(signal.SIGINT)): | |
182 | return 0 |
|
182 | signal.signal(signal.SIGINT, signal.default_int_handler) | |
183 |
|
183 | |||
184 |
|
184 | app.MainLoop() | ||
185 | if sys.platform == 'darwin': |
|
185 | ||
186 | # On OSX, evtloop.Pending() always returns True, regardless of there being |
|
186 | # Get the major wx version number to figure out what input hook we should use. | |
187 | # any events pending. As such we can't use implementations 1 or 3 of the |
|
187 | major_version = 3 | |
188 | # inputhook as those depend on a pending/dispatch loop. |
|
188 | try: | |
|
189 | major_version = int(wx.__version__[0]) | |||
|
190 | except Exception: | |||
|
191 | pass | |||
|
192 | ||||
|
193 | # Use the phoenix hook on all platforms for wxpython >= 4 | |||
|
194 | if major_version >= 4: | |||
|
195 | inputhook = inputhook_wxphoenix | |||
|
196 | # On OSX, evtloop.Pending() always returns True, regardless of there being | |||
|
197 | # any events pending. As such we can't use implementations 1 or 3 of the | |||
|
198 | # inputhook as those depend on a pending/dispatch loop. | |||
|
199 | elif sys.platform == 'darwin': | |||
189 | inputhook = inputhook_wx2 |
|
200 | inputhook = inputhook_wx2 | |
190 | else: |
|
201 | else: | |
191 |
|
202 | inputhook = inputhook_wx3 | ||
192 | # Get the major wx version number |
|
|||
193 | major_version = 3 |
|
|||
194 | try: |
|
|||
195 | major_version = int(wx.__version__[0]) |
|
|||
196 | except Exception: |
|
|||
197 | pass |
|
|||
198 |
|
||||
199 | # Use the phoenix hook for wxpython >= 4 |
|
|||
200 | if major_version >= 4: |
|
|||
201 | inputhook = inputhook_wxphoenix |
|
|||
202 | else: |
|
|||
203 | inputhook = inputhook_wx3 |
|
General Comments 0
You need to be logged in to leave comments.
Login now