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