]>
Commit | Line | Data |
---|---|---|
8a35299a RD |
1 | #!/usr/bin/env python |
2 | #--------------------------------------------------------------------------- | |
799a341c RD |
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 | ||
1fded56b | 12 | import wx # This module uses the new wx namespace |
799a341c RD |
13 | import time |
14 | ||
15 | ||
16 | #--------------------------------------------------------------------------- | |
17 | ||
1fded56b | 18 | class MyFrame(wx.Frame): |
799a341c RD |
19 | |
20 | def __init__(self, parent, id, title): | |
1fded56b RD |
21 | wx.Frame.__init__(self, parent, id, title, |
22 | wx.Point(100, 100), wx.Size(160, 150)) | |
799a341c | 23 | |
1fded56b RD |
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) | |
799a341c RD |
28 | |
29 | self.count = 0 | |
30 | ||
1fded56b RD |
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) | |
799a341c | 36 | |
1fded56b RD |
37 | wx.StaticText(panel, -1, "Idle:", |
38 | wx.DLG_PNT(panel, wx.Point(4, 28)), wx.DefaultSize) | |
799a341c | 39 | |
1fded56b RD |
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) | |
799a341c | 44 | |
1fded56b RD |
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) | |
799a341c | 49 | |
1fded56b RD |
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) | |
799a341c RD |
54 | |
55 | ||
56 | def OnCloseWindow(self, event): | |
1e4a197e | 57 | app.keepGoing = False |
799a341c RD |
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 | ||
1fded56b | 77 | class MyApp(wx.App): |
799a341c RD |
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. | |
4d5a7477 | 93 | while self.Pending(): |
799a341c RD |
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): | |
493f1553 | 106 | frame = MyFrame(None, -1, "This is a test") |
1e4a197e | 107 | frame.Show(True) |
799a341c RD |
108 | self.SetTopWindow(frame) |
109 | ||
1e4a197e | 110 | self.keepGoing = True |
799a341c | 111 | |
1e4a197e | 112 | return True |
799a341c RD |
113 | |
114 | ||
115 | app = MyApp(0) | |
116 | app.MainLoop() | |
117 | ||
118 | ||
119 | ||
120 | ||
121 |