]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/demoMainLoop.py
Moved @interface wxNSApplicationDelegate to new header
[wxWidgets.git] / wxPython / demo / demoMainLoop.py
CommitLineData
8a35299a
RD
1#!/usr/bin/env python
2#---------------------------------------------------------------------------
8fa876ca
RD
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
799a341c
RD
9"""
10This demo attempts to override the C++ MainLoop and implement it
11in Python. This is not part of the demo framework.
12
13
14 THIS FEATURE IS STILL EXPERIMENTAL...
15"""
16
799a341c 17import time
8fa876ca 18import wx
799a341c
RD
19
20#---------------------------------------------------------------------------
21
1fded56b 22class MyFrame(wx.Frame):
799a341c
RD
23
24 def __init__(self, parent, id, title):
1fded56b 25 wx.Frame.__init__(self, parent, id, title,
8fa876ca 26 (100, 100), (160, 150))
799a341c 27
8fa876ca
RD
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)
799a341c
RD
32
33 self.count = 0
34
1fded56b
RD
35 panel = wx.Panel(self, -1)
36 wx.StaticText(panel, -1, "Size:",
8fa876ca 37 wx.DLG_PNT(panel, (4, 4)), wx.DefaultSize)
1fded56b 38 wx.StaticText(panel, -1, "Pos:",
8fa876ca 39 wx.DLG_PNT(panel, (4, 16)), wx.DefaultSize)
799a341c 40
1fded56b 41 wx.StaticText(panel, -1, "Idle:",
8fa876ca 42 wx.DLG_PNT(panel, (4, 28)), wx.DefaultSize)
799a341c 43
1fded56b 44 self.sizeCtrl = wx.TextCtrl(panel, -1, "",
8fa876ca
RD
45 wx.DLG_PNT(panel, (24, 4)),
46 wx.DLG_SZE(panel, (36, -1)),
1fded56b 47 wx.TE_READONLY)
799a341c 48
1fded56b 49 self.posCtrl = wx.TextCtrl(panel, -1, "",
8fa876ca
RD
50 wx.DLG_PNT(panel, (24, 16)),
51 wx.DLG_SZE(panel, (36, -1)),
1fded56b 52 wx.TE_READONLY)
799a341c 53
1fded56b 54 self.idleCtrl = wx.TextCtrl(panel, -1, "",
8fa876ca
RD
55 wx.DLG_PNT(panel, (24, 28)),
56 wx.DLG_SZE(panel, (36, -1)),
1fded56b 57 wx.TE_READONLY)
799a341c
RD
58
59
60 def OnCloseWindow(self, event):
1e4a197e 61 app.keepGoing = False
799a341c
RD
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
1fded56b 81class MyApp(wx.App):
799a341c
RD
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.
4d5a7477 97 while self.Pending():
799a341c
RD
98 self.Dispatch()
99
8b9a4190 100 # Send idle events to idle handlers. You may want to throttle
799a341c
RD
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):
493f1553 110 frame = MyFrame(None, -1, "This is a test")
1e4a197e 111 frame.Show(True)
799a341c
RD
112 self.SetTopWindow(frame)
113
1e4a197e 114 self.keepGoing = True
799a341c 115
1e4a197e 116 return True
799a341c
RD
117
118
8fa876ca 119app = MyApp(False)
799a341c
RD
120app.MainLoop()
121
122
123
124
125