]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/mainloop/mainloop.py
wrong caption for html window, cleanup
[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 # Create an event loop and make it active. If you are
71 # only going to temporarily have a nested event loop then
72 # you should get a reference to the old one and set it as
73 # the active event loop when you are done with this one...
74 evtloop = wx.EventLoop()
75 old = wx.EventLoop.GetActive()
76 wx.EventLoop.SetActive(evtloop)
77
78 # This outer loop determines when to exit the application,
79 # for this example we let the main frame reset this flag
80 # when it closes.
81 while self.keepGoing:
82 # At this point in the outer loop you could do
83 # whatever you implemented your own MainLoop for. It
84 # should be quick and non-blocking, otherwise your GUI
85 # will freeze.
86
87 # call_your_code_here()
88
89
90 # This inner loop will process any GUI events
91 # until there are no more waiting.
92 while evtloop.Pending():
93 evtloop.Dispatch()
94
95 # Send idle events to idle handlers. You may want to
96 # throttle this back a bit somehow so there is not too
97 # much CPU time spent in the idle handlers. For this
98 # example, I'll just snooze a little...
99 time.sleep(0.10)
100 self.ProcessIdle()
101
102 wx.EventLoop.SetActive(old)
103
104
105
106 def OnInit(self):
107 frame = MyFrame(None, -1, "This is a test")
108 frame.Show(True)
109 self.SetTopWindow(frame)
110
111 self.keepGoing = True
112 return True
113
114
115 app = MyApp(False)
116 app.MainLoop()
117
118
119
120
121