Posted By

trurl on 11/29/12


Tagged


Versions (?)

mvc


 / Published in: Python
 

Minimal mvc sample code with python + wx

  1. #!/usr/bin/env python
  2.  
  3. import wx
  4. from math import sqrt
  5.  
  6. class Model:
  7. def __init__(self, callback):
  8. self._path = 0
  9. self._x = None
  10. self._y = None
  11. self._notify = callback
  12.  
  13. def updatePos(self, x,y):
  14. if self._x is not None:
  15. self._path += sqrt( (x - self._x)**2 + (y - self._y)**2 )
  16. self._x = x
  17. self._y = y
  18. self._notify()
  19.  
  20. def path(self):
  21. return self._path
  22.  
  23. class View(wx.Frame):
  24. def __init__(self):
  25. wx.Frame.__init__(self, None, title="Main View")
  26.  
  27. sizer = wx.BoxSizer(wx.VERTICAL)
  28. self._mousePath = wx.TextCtrl(self)
  29. sizer.Add(wx.StaticText(self, label="Mouse Path"), 0, wx.EXPAND | wx.ALL)
  30. sizer.Add(self._mousePath, 0, wx.EXPAND | wx.ALL)
  31. self.SetSizer(sizer)
  32.  
  33. def setPath(self, path):
  34. self._mousePath.SetValue(str(path))
  35.  
  36. class Controller:
  37. def __init__(self):
  38. self._model = Model(self._onUpdate)
  39. self._view = View()
  40. self._view.setPath(self._model.path())
  41.  
  42. self._view.Bind(wx.EVT_MOTION, self.onMouseMotion)
  43. self._view.Show()
  44.  
  45. def _onUpdate(self):
  46. self._view.setPath(self._model.path())
  47.  
  48. def onMouseMotion(self, evt):
  49. self._model.updatePos(evt.GetX(), evt.GetY())
  50.  
  51. if __name__ == "__main__":
  52. app = wx.App(False)
  53. controller = Controller()
  54. app.MainLoop()

Report this snippet  

You need to login to post a comment.