caurender.py
author Eugen Sawin <sawine@me73.com>
Thu, 30 Dec 2010 15:56:07 +0100
changeset 6 cac4ae0f22f4
parent 3 89d76549ba6e
permissions -rw-r--r--
Best Pi approximation so far.
     1 import Tkinter as tk
     2 from caugrid import Grid
     3 
     4 class Render(object):
     5 	def __init__(self, title, width, height):
     6 		self.title = title		
     7 		self.width = width
     8 		self.height = height
     9 		self.root = tk.Tk(className=title, sync=1)
    10 		self.canvas = tk.Canvas(width=self.width, height=self.height, bg="black")
    11 		self.canvas.pack(expand=tk.YES, fill=tk.BOTH)
    12 	def render(self, grid):
    13 		offset = (self.width / 2, self.height / 2)
    14 		size = 1
    15 		cell_ids = []
    16 		for pos in grid.cells.iterkeys():
    17 			x = pos[0] + offset[0]
    18 			y = pos[1] + offset[1]
    19 			cell_id = self.canvas.create_rectangle(x, y, x+size, y+size, 
    20 					fill="yellow", width=0)
    21 			cell_ids.append(cell_id)
    22 		self.root.update()
    23 		for cell_id in cell_ids:
    24 			self.canvas.delete(cell_id)
    25 
    26 import sys
    27 import time
    28 import caumarshal as marshal
    29 import os, stat
    30 
    31 def main():
    32 	filename = sys.argv[1]
    33 	print "rendering ", filename	
    34 	render = Render("hope", 1000, 1000)
    35 	mod_time = 0
    36 	while True:
    37 		current = os.stat(filename)[stat.ST_MTIME]
    38 		if current > mod_time:
    39 			mod_time = current
    40 			print "updating render"
    41 			try:
    42 				grid = marshal.loadGrid(filename)
    43 				render.render(grid)
    44 			except IndexError, ValueError:
    45 				print "loading error"
    46 		time.sleep(1)
    47 
    48 if __name__ == "__main__":
    49 	main()