File: demoMainLoop.py

package info (click to toggle)
wxwindows2.4 2.4.5.1.1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 59,920 kB
  • ctags: 97,489
  • sloc: cpp: 610,307; ansic: 111,957; python: 103,357; makefile: 3,676; sh: 3,391; lex: 192; yacc: 128; xml: 95; pascal: 74
file content (121 lines) | stat: -rwxr-xr-x 3,734 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python
#---------------------------------------------------------------------------
"""
This demo attempts to override the C++ MainLoop and implement it
in Python.  This is not part of the demo framework.


                THIS FEATURE IS STILL EXPERIMENTAL...
"""


import wx                  # This module uses the new wx namespace
import time


#---------------------------------------------------------------------------

class MyFrame(wx.Frame):

    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title,
                         wx.Point(100, 100), wx.Size(160, 150))

        wx.EVT_SIZE(self, self.OnSize)
        wx.EVT_MOVE(self, self.OnMove)
        wx.EVT_CLOSE(self, self.OnCloseWindow)
        wx.EVT_IDLE(self, self.OnIdle)

        self.count = 0

        panel = wx.Panel(self, -1)
        wx.StaticText(panel, -1, "Size:",
                     wx.DLG_PNT(panel, wx.Point(4, 4)),  wx.DefaultSize)
        wx.StaticText(panel, -1, "Pos:",
                     wx.DLG_PNT(panel, wx.Point(4, 16)), wx.DefaultSize)

        wx.StaticText(panel, -1, "Idle:",
                     wx.DLG_PNT(panel, wx.Point(4, 28)), wx.DefaultSize)

        self.sizeCtrl = wx.TextCtrl(panel, -1, "",
                                   wx.DLG_PNT(panel, wx.Point(24, 4)),
                                   wx.DLG_SZE(panel, wx.Size(36, -1)),
                                   wx.TE_READONLY)

        self.posCtrl = wx.TextCtrl(panel, -1, "",
                                  wx.DLG_PNT(panel, wx.Point(24, 16)),
                                  wx.DLG_SZE(panel, wx.Size(36, -1)),
                                  wx.TE_READONLY)

        self.idleCtrl = wx.TextCtrl(panel, -1, "",
                                   wx.DLG_PNT(panel, wx.Point(24, 28)),
                                   wx.DLG_SZE(panel, wx.Size(36, -1)),
                                   wx.TE_READONLY)


    def OnCloseWindow(self, event):
        app.keepGoing = False
        self.Destroy()

    def OnIdle(self, event):
        self.idleCtrl.SetValue(str(self.count))
        self.count = self.count + 1

    def OnSize(self, event):
        size = event.GetSize()
        self.sizeCtrl.SetValue("%s, %s" % (size.width, size.height))
        event.Skip()

    def OnMove(self, event):
        pos = event.GetPosition()
        self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y))



#---------------------------------------------------------------------------

class MyApp(wx.App):
    def MainLoop(self):
        # This outer loop determines when to exit the application,  for
        # this example we let the main frame reset this flag when it
        # closes.
        while self.keepGoing:
            # At this point in the outer loop you could do whatever you
            # implemented your own MainLoop for.  It should be quick and
            # non-blocking, otherwise your GUI will freeze.  For example,
            # call Fnorb's reactor.do_one_event(0), etc.

            # call_your_code_here()


            # This inner loop will process any GUI events until there
            # are no more waiting.
            while self.Pending():
                self.Dispatch()

            # Send idle events to idle handlers.  You may want to throttle
            # this back a bit so there is not too much CPU time spent in
            # the idle handlers.  For this example, I'll just snooze a
            # little...
            time.sleep(0.25)
            self.ProcessIdle()



    def OnInit(self):
        frame = MyFrame(None, -1, "This is a test")
        frame.Show(True)
        self.SetTopWindow(frame)

        self.keepGoing = True

        return True


app = MyApp(0)
app.MainLoop()