| 1 | #!/usr/bin/env python |
| 2 | #--------------------------------------------------------------------------- |
| 3 | """ |
| 4 | This demo attempts to override the C++ MainLoop and implement it |
| 5 | in Python. This is not part of the demo framework. |
| 6 | |
| 7 | |
| 8 | THIS FEATURE IS STILL EXPERIMENTAL... |
| 9 | """ |
| 10 | |
| 11 | |
| 12 | import wx # This module uses the new wx namespace |
| 13 | import time |
| 14 | |
| 15 | |
| 16 | #--------------------------------------------------------------------------- |
| 17 | |
| 18 | class MyFrame(wx.Frame): |
| 19 | |
| 20 | def __init__(self, parent, id, title): |
| 21 | wx.Frame.__init__(self, parent, id, title, |
| 22 | wx.Point(100, 100), wx.Size(160, 150)) |
| 23 | |
| 24 | wx.EVT_SIZE(self, self.OnSize) |
| 25 | wx.EVT_MOVE(self, self.OnMove) |
| 26 | wx.EVT_CLOSE(self, self.OnCloseWindow) |
| 27 | wx.EVT_IDLE(self, self.OnIdle) |
| 28 | |
| 29 | self.count = 0 |
| 30 | |
| 31 | panel = wx.Panel(self, -1) |
| 32 | wx.StaticText(panel, -1, "Size:", |
| 33 | wx.DLG_PNT(panel, wx.Point(4, 4)), wx.DefaultSize) |
| 34 | wx.StaticText(panel, -1, "Pos:", |
| 35 | wx.DLG_PNT(panel, wx.Point(4, 16)), wx.DefaultSize) |
| 36 | |
| 37 | wx.StaticText(panel, -1, "Idle:", |
| 38 | wx.DLG_PNT(panel, wx.Point(4, 28)), wx.DefaultSize) |
| 39 | |
| 40 | self.sizeCtrl = wx.TextCtrl(panel, -1, "", |
| 41 | wx.DLG_PNT(panel, wx.Point(24, 4)), |
| 42 | wx.DLG_SZE(panel, wx.Size(36, -1)), |
| 43 | wx.TE_READONLY) |
| 44 | |
| 45 | self.posCtrl = wx.TextCtrl(panel, -1, "", |
| 46 | wx.DLG_PNT(panel, wx.Point(24, 16)), |
| 47 | wx.DLG_SZE(panel, wx.Size(36, -1)), |
| 48 | wx.TE_READONLY) |
| 49 | |
| 50 | self.idleCtrl = wx.TextCtrl(panel, -1, "", |
| 51 | wx.DLG_PNT(panel, wx.Point(24, 28)), |
| 52 | wx.DLG_SZE(panel, wx.Size(36, -1)), |
| 53 | wx.TE_READONLY) |
| 54 | |
| 55 | |
| 56 | def OnCloseWindow(self, event): |
| 57 | app.keepGoing = False |
| 58 | self.Destroy() |
| 59 | |
| 60 | def OnIdle(self, event): |
| 61 | self.idleCtrl.SetValue(str(self.count)) |
| 62 | self.count = self.count + 1 |
| 63 | |
| 64 | def OnSize(self, event): |
| 65 | size = event.GetSize() |
| 66 | self.sizeCtrl.SetValue("%s, %s" % (size.width, size.height)) |
| 67 | event.Skip() |
| 68 | |
| 69 | def OnMove(self, event): |
| 70 | pos = event.GetPosition() |
| 71 | self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y)) |
| 72 | |
| 73 | |
| 74 | |
| 75 | #--------------------------------------------------------------------------- |
| 76 | |
| 77 | class MyApp(wx.App): |
| 78 | def MainLoop(self): |
| 79 | # This outer loop determines when to exit the application, for |
| 80 | # this example we let the main frame reset this flag when it |
| 81 | # closes. |
| 82 | while self.keepGoing: |
| 83 | # At this point in the outer loop you could do whatever you |
| 84 | # implemented your own MainLoop for. It should be quick and |
| 85 | # non-blocking, otherwise your GUI will freeze. For example, |
| 86 | # call Fnorb's reactor.do_one_event(0), etc. |
| 87 | |
| 88 | # call_your_code_here() |
| 89 | |
| 90 | |
| 91 | # This inner loop will process any GUI events until there |
| 92 | # are no more waiting. |
| 93 | while self.Pending(): |
| 94 | self.Dispatch() |
| 95 | |
| 96 | # Send idle events to idle handlers. You may want to throtle |
| 97 | # this back a bit so there is not too much CPU time spent in |
| 98 | # the idle handlers. For this example, I'll just snooze a |
| 99 | # little... |
| 100 | time.sleep(0.25) |
| 101 | self.ProcessIdle() |
| 102 | |
| 103 | |
| 104 | |
| 105 | def OnInit(self): |
| 106 | frame = MyFrame(None, -1, "This is a test") |
| 107 | frame.Show(True) |
| 108 | self.SetTopWindow(frame) |
| 109 | |
| 110 | self.keepGoing = True |
| 111 | |
| 112 | return True |
| 113 | |
| 114 | |
| 115 | app = MyApp(0) |
| 116 | app.MainLoop() |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |