For a quick overview of Model/View (and sometimes Controller)
take a look at the Apple web site. their default GUI design uses
the paradigm and there is a good visual description in the
overview:
http://developer.apple.com/documentation/Cocoa/Conceptual/ObjCTutorial/chapter02/chapter_2_section_3.html
For a slightly different approach try:
http://www.mimuw.edu.pl/~sl/teaching/00_01/Delfin_EC/Overviews/ModelViewPresenter.htm
# -*- coding: windows-1251 -*-
import random, time, Tkinter
class Agent:
def __init__ (self, x=100, y=100):
self.name = random.randint(0, 100000)
self.x = x
self.y = y
def __repr__ (self):
return str(self.name)
def display(self, canvas, x, y):
"""Display an image of this Agent on the canvas."""
canvas.coords(self.ID, x, y)
def rand_vec(self):
return random.randint(-5, 5), random.randint(-5, 5)
class Environment:
def __init__ (self):
self.alive_agents = []
def step(self):
for obj in self.alive_agents:
old_x, old_y = obj.x, obj.y
change_x, change_y = obj.rand_vec()
obj.x, obj.y = old_x + change_x, old_y + change_y
# print obj.name, '\t:\t ', old_x, old_y,' -> ', obj.x, obj.y
def add_agent(self, agent):
# print agent.name, ' was added to environment'
self.alive_agents.append
(agent)
class GraphEnvironment(Tkinter.Frame):
def __init__ (self, root):
self.root = root
self.background = "" width=200, height=200, background="">
for agent in env.alive_agents:
agent.ID=self.background.create_image
(100, 100, anchor=Tkinter.NW, image=picture)
self.background.pack(fill=Tkinter.BOTH,expand=Tkinter.YES)
self.root.after(100,self.NextDay)
def NextDay(self):
env.step()
for agent in env.alive_agents:
agent.display(self.background, agent.x, agent.y)
self.root.after(200,self.NextDay)
print '\tBORN'
env = Environment()
for obj in range(100):
child = Agent()
# print child, ' was born'
env.add_agent(child)
print '\tGRAPH LIFE'
root=Tkinter.Tk()
picture =Tkinter.PhotoImage(file="HolyGrail.gif")
GraphEnvironment(root)
root.mainloop()
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor