| 241 | |
| 242 | |
| 243 | class CefApp(wx.App): |
| 244 | |
| 245 | def __init__(self, redirect): |
| 246 | self.timer = None |
| 247 | self.timer_id = 1 |
| 248 | self.is_initialized = False |
| 249 | super(CefApp, self).__init__(redirect=redirect) |
| 250 | |
| 251 | def OnPreInit(self): |
| 252 | super(CefApp, self).OnPreInit() |
| 253 | # On Mac with wxPython 4.0 the OnInit() event never gets |
| 254 | # called. Doing wx window creation in OnPreInit() seems to |
| 255 | # resolve the problem (Issue #350). |
| 256 | if MAC and wx.version().startswith("4."): |
| 257 | print("[wxpython.py] OnPreInit: initialize here" |
| 258 | " (wxPython 4.0 fix)") |
| 259 | self.initialize() |
| 260 | |
| 261 | def OnInit(self): |
| 262 | self.initialize() |
| 263 | return True |
| 264 | |
| 265 | def initialize(self): |
| 266 | if self.is_initialized: |
| 267 | return |
| 268 | self.is_initialized = True |
| 269 | self.create_timer() |
| 270 | frame = MainFrame() |
| 271 | self.SetTopWindow(frame) |
| 272 | frame.Show() |
| 273 | |
| 274 | def create_timer(self): |
| 275 | # See also "Making a render loop": |
| 276 | # http://wiki.wxwidgets.org/Making_a_render_loop |
| 277 | # Another way would be to use EVT_IDLE in MainFrame. |
| 278 | self.timer = wx.Timer(self, self.timer_id) |
| 279 | self.Bind(wx.EVT_TIMER, self.on_timer, self.timer) |
| 280 | self.timer.Start(10) # 10ms timer |
| 281 | |
| 282 | def on_timer(self, _): |
| 283 | cef.MessageLoopWork() |
| 284 | |
| 285 | def OnExit(self): |
| 286 | self.timer.Stop() |
| 287 | return 0 |
| 288 | |
| 289 | |
| 290 | if __name__ == '__main__': |