# This program creates a single cell at the top left corner of a wxPython window, and # flickers it. At every step, the value of 'cell_state', a scalar variable, is incremented. Then, # depending on whether the value is odd or even, the color of the cell is determined, and # a fresh cell is drawn with the new paint color. # You may find this code useful when your CA has a 2D space, e.g., 'game of life'. # Thus, at every time step the state of the 2D CA changes, and therefore you would have to # paint a fresh 2D grid at each step. # NOTE: As in '2Dcells.py', you have to write logic to populate 'cell_state' here. In this program, # 'cell_state' is a simple scalar variable that holds a single number. For the game of life problem, # 'cell_states' would have to be a matrix just like the one in '2Dcells.py'. In that program, you # populate 'cell_states' only once since it represents the full space-time diagram, and paint it once. # In the case of game of life, the 'cell_states' would represent a single state of the 2 dimensional CA. # wxPython NOTE: Most of the code here is similar to '2Dcells.py' provided for the 1st problem. # This program has two additional things: 'Timer' and 'PostEvent'. The flickering happens every half # second, which is controlled by a 'Timer' whose event is bound to the 'Update' method. In that # method, the cell is freshly painted. Once the painting is done, it explicitly generates a 'EVT_PAINT' # event in order to invoke the 'OnPaint' method. In that method, a new timer is set that is stopped # in the 'Update' method. import wx app = wx.App() frame = wx.Frame(None, title="Draw on Panel", size = (640, 480)) panel = wx.Panel(frame) panel.SetBackgroundColour("black") def OnPaint(event): panel.dc = wx.PaintDC(panel) timer.Start(500) def Update(event): if (frame.cell_state % 2 == 0): col = "red" else: col = "green" frame.cell_state += 1 panel.dc.SetBrush(wx.Brush(col, wx.SOLID)) panel.dc.DrawRectangle(0, 0, 50, 50) timer.Stop() wx.PostEvent(panel, wx.PaintEvent()) frame.cell_state = 0 panel.Bind(wx.EVT_PAINT, OnPaint) timer = wx.Timer(panel) panel.Bind(wx.EVT_TIMER, Update, timer) frame.Center() frame.Show(True) # A mainloop is an endless cycle that catches up all events coming up to your application. # It is an integral part of any windows GUI application. app.MainLoop()