Asynchronous monitoring service for wx applications.
Define a monitor using park.wxmonitor.wxMonitor(panel) where panel is
the window which will receive the monitor updates.
In panel, be sure to have methods for onMonitorStart(message),
onMonitorProgress(message), etc., for the kinds of monitor messages
the application will send. The catch-all method is onMonitorMessage,
which by default will print the messages on the console. If you
don't catch onMonitorLog messages then the log messages will be
sent to the standard python logger.
Example
The following defines a panel which responds to monitor messages:
import wx
class Panel(wx.Panel):
def __init__(self, *args, **kw):
wx.Panel.__init__(self, *args, **kw)
self.text = wx.TextCtrl(self, size=(200,100), style=wx.TE_MULTILINE)
self.gauge = wx.Gauge(self, range=100)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.text, 0, wx.LEFT | wx.EXPAND)
sizer.Add(self.gauge, 0, wx.LEFT | wx.EXPAND)
self.SetSizer(sizer)
self.text.SetValue('starting value')
def onMonitorMessage(self, message):
self.text.SetValue(str(message))
def onMonitorStart(self, message):
self.text.SetValue(str(message))
self.gauge.SetValue(0)
def onMonitorProgress(self, message):
self.text.SetValue(str(message))
self.gauge.SetValue(int(100*message.complete/message.total))
def onMonitorComplete(self, message):
self.text.SetValue(str(message))
self.gauge.SetValue(100)
We can put this panel in a simple app:
app = wx.PySimpleApp()
frame = wx.Frame(None, -1, 'Test Monitor')
panel = Panel(frame)
frame.Show()
Next we attach attach the monitor to this panel and feed some messages from
another thread:
import time,thread
import park.wxmonitor, park.monitor
from park.monitor import Start, Progress, Improvement, Complete
monitor = park.wxmonitor.wxMonitor(panel)
msgs = [Start(), Progress(1,10), Progress(3,10),
Improvement('Better!'), Progerss(6,10), Complete('Best!')]:
def message_stream(monitor,msgs):
time.sleep(1)
for message in msgs:
monitor.put(message)
time.sleep(1)
thread.start_new_thread(message_stream, (monitor,msgs))
app.MainLoop()
You should see the progress bar jump from 10% to 30% to 60% then all the way
to the end.