| 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 | from wxPython.wx import * |
| 13 | import time |
| 14 | |
| 15 | |
| 16 | #--------------------------------------------------------------------------- |
| 17 | |
| 18 | class MyFrame(wxFrame): |
| 19 | |
| 20 | def __init__(self, parent, id, title): |
| 21 | wxFrame.__init__(self, parent, id, title, |
| 22 | wxPoint(100, 100), wxSize(160, 150)) |
| 23 | |
| 24 | EVT_SIZE(self, self.OnSize) |
| 25 | EVT_MOVE(self, self.OnMove) |
| 26 | EVT_CLOSE(self, self.OnCloseWindow) |
| 27 | EVT_IDLE(self, self.OnIdle) |
| 28 | |
| 29 | self.count = 0 |
| 30 | |
| 31 | panel = wxPanel(self, -1) |
| 32 | wxStaticText(panel, -1, "Size:", |
| 33 | wxDLG_PNT(panel, wxPoint(4, 4)), wxDefaultSize) |
| 34 | wxStaticText(panel, -1, "Pos:", |
| 35 | wxDLG_PNT(panel, wxPoint(4, 16)), wxDefaultSize) |
| 36 | |
| 37 | wxStaticText(panel, -1, "Idle:", |
| 38 | wxDLG_PNT(panel, wxPoint(4, 28)), wxDefaultSize) |
| 39 | |
| 40 | self.sizeCtrl = wxTextCtrl(panel, -1, "", |
| 41 | wxDLG_PNT(panel, wxPoint(24, 4)), |
| 42 | wxDLG_SZE(panel, wxSize(36, -1)), |
| 43 | wxTE_READONLY) |
| 44 | |
| 45 | self.posCtrl = wxTextCtrl(panel, -1, "", |
| 46 | wxDLG_PNT(panel, wxPoint(24, 16)), |
| 47 | wxDLG_SZE(panel, wxSize(36, -1)), |
| 48 | wxTE_READONLY) |
| 49 | |
| 50 | self.idleCtrl = wxTextCtrl(panel, -1, "", |
| 51 | wxDLG_PNT(panel, wxPoint(24, 28)), |
| 52 | wxDLG_SZE(panel, wxSize(36, -1)), |
| 53 | wxTE_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(wxApp): |
| 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 | |