[Tutor] visualization

Alan Gauld alan.gauld at freenet.co.uk
Sat Jul 29 10:17:45 CEST 2006


> class Object:

Probably good to change the name, there is a built in class
"object" and this could cause confusion...

>    def __init__ (self, x=10, y=10):
>    def __repr__ (self):
>    def display(self, canvas, x, y, width, height):
>    def rand_vec(self):
>
>
> class Environment:
>    def __init__ (self):
>    def step(self):
>            print obj.name, '\t:\t ', old_x, old_y,' -> ', obj.x,

The print statements will go to a console if you convert to
a GUI. |Since they are basically debug statements
that shouldn't be a problem, but you may prefer to
display them in a message widget instead...

>    def run(self, steps=10):
>    def add_object(self, object):

> print '\tBORN'
> env = Environment()
> for obj in range(10):
>    child = Object()
>    print child, ' was born'
>    env.add_object(child)
>
> print '\tLIFE'
> env.run(100)

> And now I want to visualizate this world.
> What modules can you advise me for "easy" visualization?

You already import Tkinter so presumabnly you want to use
that as your GUI? What other kinds of modules do you want?
You already have a display method in your object so you
just need to write the code to display something - a small
image maybe? - on a canvas.

You will need to create a module that draws the GUI and
handles the user events. That module will probably
import this one (rather than this one importing Tkinter)
You can then just pass the canvas object to each object
in your environment and ask it to draw itself...

You might like to separate your objects into model and
view representations but thats getting into more advanced
GUI design and probably not needed here.

> "Easy" means that it would be really nice if I could do 
> vizualization with
> minimum change of existing classes.

The golden rule is always to avoid writing code that mixes
display and computation. That is to avoid using print statements
inside the core objects etc.

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



More information about the Tutor mailing list