J
John Salerno
The code to look at is the try statement in the NumbersValidator class,
just a few lines down. Is this a clean way to write it? i.e. is it okay
to have all those return statements? Is this a good use of try? Etc.
Thanks.
----------------------------
import wx
class NumbersValidator(wx.PyValidator):
def __init__(self):
wx.PyValidator.__init__(self)
def Clone(self):
return NumbersValidator()
def Validate(self, parent):
text_ctrl = self.GetWindow()
text = text_ctrl.GetValue()
try:
if not text or int(text) <= 0:
wx.MessageBox('Enter a valid time.', 'Invalid time
entered', wx.OK | wx.ICON_ERROR)
return False
else:
return True
except ValueError, error:
wx.MessageBox('Enter a valid time.', 'Invalid time entered',
wx.OK | wx.ICON_ERROR)
return False
def TransferToWindow(self):
return True
def TransferFromWindow(self):
return True
class Timer(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, 'Timer',
style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER
^ wx.MAXIMIZE_BOX)
self.panel = wx.Panel(self)
mainSizer = wx.BoxSizer(wx.VERTICAL)
inputSizer = wx.BoxSizer(wx.HORIZONTAL)
self.progress = wx.Gauge(self.panel, wx.ID_ANY, 100, size=(300,
20))
self.status = wx.StaticText(self.panel, wx.ID_ANY, 'Enter a time.')
prompt = wx.StaticText(self.panel, wx.ID_ANY, 'Time to wait:')
self.input = wx.TextCtrl(self.panel, wx.ID_ANY, size=(20, 20),
validator=NumbersValidator())
self.start = wx.Button(self.panel, wx.ID_ANY, 'Start')
self.reset = wx.Button(self.panel, wx.ID_ANY, 'Reset')
self.reset.Disable()
self.sound = wx.Sound(r'C:\Windows\Media\notify.wav')
self.timer = wx.Timer(self)
mainSizer.Add(self.progress, flag=wx.ALIGN_CENTER | wx.ALL ^
wx.BOTTOM,
border=10)
mainSizer.Add(self.status, flag=wx.ALIGN_CENTER | wx.ALL,
border=10)
mainSizer.Add(inputSizer, flag=wx.ALIGN_CENTER | wx.BOTTOM,
border=10)
inputSizer.Add(prompt, flag=wx.ALIGN_CENTER)
inputSizer.Add(self.input, flag=wx.ALIGN_CENTER | wx.LEFT |
wx.RIGHT,
border=5)
inputSizer.Add(self.start, flag=wx.ALIGN_CENTER)
inputSizer.Add(self.reset, flag=wx.ALIGN_CENTER)
self.Bind(wx.EVT_TEXT_ENTER, self.OnStart, self.input)
self.Bind(wx.EVT_BUTTON, self.OnStart, self.start)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.Bind(wx.EVT_BUTTON, self.OnReset, self.reset)
self.panel.SetSizer(mainSizer)
mainSizer.Fit(self)
def OnStart(self, event):
if self.panel.Validate():
self.time = int(self.input.GetValue())
self.minutes_passed = 1
self.minutes_remaining = self.time - 1
self.start.Disable()
self.input.Disable()
self.reset.Enable()
self.status.SetLabel('%s minute(s) remaining.' % self.time)
self.timer.Start(1000)
def OnReset(self, event):
if self.timer.IsRunning():
self.timer.Stop()
self.input.Clear()
self.input.Enable()
self.start.Enable()
self.reset.Disable()
self.status.SetLabel('Enter a new time.')
self.progress.SetValue(0)
self.minutes_passed = 1
def OnTimer(self, event):
if self.minutes_remaining != 0:
self.progress.SetValue(self.minutes_passed * 100 / self.time)
self.status.SetLabel('%s minute(s) remaining.' %
self.minutes_remaining)
self.minutes_passed += 1
self.minutes_remaining -= 1
else:
self.timer.Stop()
self.progress.SetValue(self.minutes_passed * 100 / self.time)
self.status.SetLabel('%s minute(s) have elapsed.' % self.time)
wx.Sound.Play(self.sound)
class MyApp(wx.App):
def OnInit(self):
frame = Timer()
self.SetTopWindow(frame)
frame.Show()
return True
if __name__ == '__main__':
app = MyApp(False)
app.MainLoop()
just a few lines down. Is this a clean way to write it? i.e. is it okay
to have all those return statements? Is this a good use of try? Etc.
Thanks.
----------------------------
import wx
class NumbersValidator(wx.PyValidator):
def __init__(self):
wx.PyValidator.__init__(self)
def Clone(self):
return NumbersValidator()
def Validate(self, parent):
text_ctrl = self.GetWindow()
text = text_ctrl.GetValue()
try:
if not text or int(text) <= 0:
wx.MessageBox('Enter a valid time.', 'Invalid time
entered', wx.OK | wx.ICON_ERROR)
return False
else:
return True
except ValueError, error:
wx.MessageBox('Enter a valid time.', 'Invalid time entered',
wx.OK | wx.ICON_ERROR)
return False
def TransferToWindow(self):
return True
def TransferFromWindow(self):
return True
class Timer(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, 'Timer',
style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER
^ wx.MAXIMIZE_BOX)
self.panel = wx.Panel(self)
mainSizer = wx.BoxSizer(wx.VERTICAL)
inputSizer = wx.BoxSizer(wx.HORIZONTAL)
self.progress = wx.Gauge(self.panel, wx.ID_ANY, 100, size=(300,
20))
self.status = wx.StaticText(self.panel, wx.ID_ANY, 'Enter a time.')
prompt = wx.StaticText(self.panel, wx.ID_ANY, 'Time to wait:')
self.input = wx.TextCtrl(self.panel, wx.ID_ANY, size=(20, 20),
validator=NumbersValidator())
self.start = wx.Button(self.panel, wx.ID_ANY, 'Start')
self.reset = wx.Button(self.panel, wx.ID_ANY, 'Reset')
self.reset.Disable()
self.sound = wx.Sound(r'C:\Windows\Media\notify.wav')
self.timer = wx.Timer(self)
mainSizer.Add(self.progress, flag=wx.ALIGN_CENTER | wx.ALL ^
wx.BOTTOM,
border=10)
mainSizer.Add(self.status, flag=wx.ALIGN_CENTER | wx.ALL,
border=10)
mainSizer.Add(inputSizer, flag=wx.ALIGN_CENTER | wx.BOTTOM,
border=10)
inputSizer.Add(prompt, flag=wx.ALIGN_CENTER)
inputSizer.Add(self.input, flag=wx.ALIGN_CENTER | wx.LEFT |
wx.RIGHT,
border=5)
inputSizer.Add(self.start, flag=wx.ALIGN_CENTER)
inputSizer.Add(self.reset, flag=wx.ALIGN_CENTER)
self.Bind(wx.EVT_TEXT_ENTER, self.OnStart, self.input)
self.Bind(wx.EVT_BUTTON, self.OnStart, self.start)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.Bind(wx.EVT_BUTTON, self.OnReset, self.reset)
self.panel.SetSizer(mainSizer)
mainSizer.Fit(self)
def OnStart(self, event):
if self.panel.Validate():
self.time = int(self.input.GetValue())
self.minutes_passed = 1
self.minutes_remaining = self.time - 1
self.start.Disable()
self.input.Disable()
self.reset.Enable()
self.status.SetLabel('%s minute(s) remaining.' % self.time)
self.timer.Start(1000)
def OnReset(self, event):
if self.timer.IsRunning():
self.timer.Stop()
self.input.Clear()
self.input.Enable()
self.start.Enable()
self.reset.Disable()
self.status.SetLabel('Enter a new time.')
self.progress.SetValue(0)
self.minutes_passed = 1
def OnTimer(self, event):
if self.minutes_remaining != 0:
self.progress.SetValue(self.minutes_passed * 100 / self.time)
self.status.SetLabel('%s minute(s) remaining.' %
self.minutes_remaining)
self.minutes_passed += 1
self.minutes_remaining -= 1
else:
self.timer.Stop()
self.progress.SetValue(self.minutes_passed * 100 / self.time)
self.status.SetLabel('%s minute(s) have elapsed.' % self.time)
wx.Sound.Play(self.sound)
class MyApp(wx.App):
def OnInit(self):
frame = Timer()
self.SetTopWindow(frame)
frame.Show()
return True
if __name__ == '__main__':
app = MyApp(False)
app.MainLoop()