]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/mainloop/mainloop.py
src/common/wxchar.cpps/wxConvLocal/wxConvLibs/g as otherwise we may fail to convert...
[wxWidgets.git] / wxPython / samples / mainloop / mainloop.py
1 #!/usr/bin/env python
2
3 """
4 This demo attempts to override the C++ MainLoop and implement it
5 in Python.
6 """
7
8 import time
9 import wx
10
11 #---------------------------------------------------------------------------
12
13 class MyFrame(wx.Frame):
14
15 def __init__(self, parent, id, title):
16 wx.Frame.__init__(self, parent, id, title,
17 (100, 100), (160, 150))
18
19 self.Bind(wx.EVT_SIZE, self.OnSize)
20 self.Bind(wx.EVT_MOVE, self.OnMove)
21 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
22 self.Bind(wx.EVT_IDLE, self.OnIdle)
23
24 self.count = 0
25
26 panel = wx.Panel(self)
27 sizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5)
28
29 self.sizeCtrl = wx.TextCtrl(panel, -1, "", style=wx.TE_READONLY)
30 sizer.Add(wx.StaticText(panel, -1, "Size:"))
31 sizer.Add(self.sizeCtrl)
32
33 self.posCtrl = wx.TextCtrl(panel, -1, "", style=wx.TE_READONLY)
34 sizer.Add(wx.StaticText(panel, -1, "Pos:"))
35 sizer.Add(self.posCtrl)
36
37 self.idleCtrl = wx.TextCtrl(panel, -1, "", style=wx.TE_READONLY)
38 sizer.Add(wx.StaticText(panel, -1, "Idle:"))
39 sizer.Add(self.idleCtrl)
40
41 border = wx.BoxSizer()
42 border.Add(sizer, 0, wx.ALL, 20)
43 panel.SetSizer(border)
44
45
46 def OnCloseWindow(self, event):
47 app.keepGoing = False
48 self.Destroy()
49
50 def OnIdle(self, event):
51 self.idleCtrl.SetValue(str(self.count))
52 self.count = self.count + 1
53
54 def OnSize(self, event):
55 size = event.GetSize()
56 self.sizeCtrl.SetValue("%s, %s" % (size.width, size.height))
57 event.Skip()
58
59 def OnMove(self, event):
60 pos = event.GetPosition()
61 self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y))
62
63
64
65 #---------------------------------------------------------------------------
66
67 class MyApp(wx.App):
68 def MainLoop(self):
69
70 if "wxMac" in wx.PlatformInfo:
71 # TODO: Does wxMac implement wxEventLoop yet???
72 wx.App.MainLoop()
73
74 else:
75 # Create an event loop and make it active. If you are
76 # only going to temporarily have a nested event loop then
77 # you should get a reference to the old one and set it as
78 # the active event loop when you are done with this one...
79 evtloop = wx.EventLoop()
80 old = wx.EventLoop.GetActive()
81 wx.EventLoop.SetActive(evtloop)
82
83 # This outer loop determines when to exit the application,
84 # for this example we let the main frame reset this flag
85 # when it closes.
86 while self.keepGoing:
87 # At this point in the outer loop you could do
88 # whatever you implemented your own MainLoop for. It
89 # should be quick and non-blocking, otherwise your GUI
90 # will freeze.
91
92 # call_your_code_here()
93
94
95 # This inner loop will process any GUI events
96 # until there are no more waiting.
97 while evtloop.Pending():
98 evtloop.Dispatch()
99
100 # Send idle events to idle handlers. You may want to
101 # throttle this back a bit somehow so there is not too
102 # much CPU time spent in the idle handlers. For this
103 # example, I'll just snooze a little...
104 time.sleep(0.10)
105 self.ProcessIdle()
106
107 wx.EventLoop.SetActive(old)
108
109
110
111 def OnInit(self):
112 frame = MyFrame(None, -1, "This is a test")
113 frame.Show(True)
114 self.SetTopWindow(frame)
115
116 self.keepGoing = True
117 return True
118
119
120 app = MyApp(False)
121 app.MainLoop()
122
123
124
125
126