[Tutor] visualization

Сергій kyxaxa at gmail.com
Sun Jul 30 01:52:38 CEST 2006


>
> 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
>


Thanks a lot, Alan.
Your links and example (and a wonderful tutorial on Tkinter
http://doctormickey.com/python/pythontutorial_100.htm) really helped me!

This is the working version of my visualization:




# -*- 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 = Tkinter.Canvas(self.root, width=200, height=200,
background="white")

        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()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060730/d7be83b5/attachment-0001.html 


More information about the Tutor mailing list