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