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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
#!/usr/bin/env python
"""
This demo attempts to override the C++ MainLoop and implement it
in Python.
"""
import time
import wx
import wx.lib.newevent as ne
##import os; raw_input('PID: %d\nPress enter...' % os.getpid())
GooEvent, EVT_GOO = ne.NewCommandEvent()
#---------------------------------------------------------------------------
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(300, 200))
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_MOVE, self.OnMove)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.Bind(wx.EVT_IDLE, self.OnIdle)
self.Bind(wx.EVT_BUTTON, self.OnButton)
self.Bind(EVT_GOO, self.OnGoo, id=123)
self.count = 0
panel = wx.Panel(self)
sizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5)
self.sizeCtrl = wx.TextCtrl(panel, -1, "", style=wx.TE_READONLY)
sizer.Add(wx.StaticText(panel, -1, "Size:"))
sizer.Add(self.sizeCtrl)
self.posCtrl = wx.TextCtrl(panel, -1, "", style=wx.TE_READONLY)
sizer.Add(wx.StaticText(panel, -1, "Pos:"))
sizer.Add(self.posCtrl)
self.idleCtrl = wx.TextCtrl(panel, -1, "", style=wx.TE_READONLY)
sizer.Add(wx.StaticText(panel, -1, "Idle:"))
sizer.Add(self.idleCtrl)
btn = wx.Button(panel, label='PostEvent')
sizer.Add(1,1)
sizer.Add(btn)
border = wx.BoxSizer()
border.Add(sizer, 0, wx.ALL, 20)
panel.SetSizer(border)
def OnCloseWindow(self, event):
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))
def OnButton(self, evt):
evt = GooEvent(id=123)
wx.PostEvent(self, evt)
def OnGoo(self, evt):
print('got EVT_GOO')
#---------------------------------------------------------------------------
class MyEventLoop(wx.GUIEventLoop):
def __init__(self):
wx.GUIEventLoop.__init__(self)
self.exitCode = 0
self.shouldExit = False
def DoMyStuff(self):
# Do whatever you want to have done for each iteration of the event
# loop. In this example we'll just sleep a bit to simulate something
# real happening.
time.sleep(0.05)
def Run(self):
# Set this loop as the active one. It will automatically reset to the
# original evtloop when the context manager exits.
with wx.EventLoopActivator(self):
while True:
self.DoMyStuff()
# Generate and process idles events for as long as there
# isn't anything else to do
while not self.shouldExit and not self.Pending() and self.ProcessIdle():
pass
if self.shouldExit:
break
# Dispatch all the pending events
self.ProcessEvents()
# Currently on wxOSX Pending always returns true, so the
# ProcessIdle above is not ever called. Call it here instead.
if 'wxOSX' in wx.PlatformInfo:
self.ProcessIdle()
# Process remaining queued messages, if any
while True:
checkAgain = False
if wx.GetApp() and wx.GetApp().HasPendingEvents():
wx.GetApp().ProcessPendingEvents()
checkAgain = True
if 'wxOSX' not in wx.PlatformInfo and self.Pending():
self.Dispatch()
checkAgain = True
if not checkAgain:
break
return self.exitCode
def Exit(self, rc=0):
self.exitCode = rc
self.shouldExit = True
self.OnExit()
self.WakeUp()
def ProcessEvents(self):
if wx.GetApp():
wx.GetApp().ProcessPendingEvents()
if self.shouldExit:
return False
return self.Dispatch()
class MyApp(wx.App):
def MainLoop(self):
self.SetExitOnFrameDelete(True)
self.mainLoop = MyEventLoop()
self.mainLoop.Run()
def ExitMainLoop(self):
self.mainLoop.Exit()
def OnInit(self):
frame = MyFrame(None, -1, "This is a test")
frame.Show(True)
self.SetTopWindow(frame)
#self.keepGoing = True
return True
app = MyApp(False)
app.MainLoop()
|