Demo entry 1076531



Submitted by anonymous on Jan 15, 2015 at 19:31
Language: Python. Code size: 6.4 kB.

"""This module is part of Swampy, a suite of programs available from

Copyright 2011 Allen B. Downey
Distributed under the GNU General Public License at

import math
import random
import time

from Tkinter import END
from World import World, Animal, MyThread

class AmoebaWorld(World):
    """A microscope slide where Amoebas trace parametric equations.

        delay: time step in ms

    def __init__(self, interactive=False, delay=100):
        self.delay = delay
        self.running = False

        if interactive:

    def make_canvas(self, low=-20, high=20):
        """Makes the canvas and draws the grid marks."""
        self.ca_width = 400
        self.ca_height = 400
        self.canvas =, height=self.ca_height,
                              bg='white', scale=[10,10])

        # draw the grid
        d = {True:'', False:'.'}
        xmin, xmax = low, high
        ymin, ymax = low, high
        for x in range(xmin, xmax+1, 1):
            self.canvas.line([[x, ymin], [x, ymax]], dash=d[x==0])
        for y in range(ymin, ymax+1, 1):
            self.canvas.line([[xmin, y], [xmax, y]], dash=d[y==0])

    def make_control_panel(self):
        """Makes the buttons and input fields."""
        # buttons
        self.bu(text='Stop', command=self.stop)
        self.bu(text='Clear', command=self.clear)
        self.bu(text='Quit', command=self.quit)

        # end time entry
        self.row([0,1,0], pady=5)'end time')
        self.en_end = self.en(width=5, text='10')'seconds')

        #make another row
        self.row([0,2,0],pady=5)"New row")
        self.en_end= self.en(width=5, text='10')'new stuff')

        # entries for x(t) and y(t)
        self.en_x_t = self.make_entry('Y_1= ')
        self.en_y_t = self.make_entry('Y_2 = ')

    def make_entry(self, label):
        """Makes an entry with the given label."""
        self.row([0,1,0], pady=5)
        entry = self.en(width=5, text=' t')
        return entry

    def set_entry(self, entry, text):
        """Sets the contents of an entry widget."""
        entry.delete(0, END)
        entry.insert(END, text)

    def set_end_time(self, text):
        """Sets the contents of the end time entry widget."""
        self.set_entry(self.en_end, text)

    def set_x_t(self, text):
        """Sets the contents of the x_t entry widget."""
        self.set_entry(self.en_x_t, text)

    def set_y_t(self, text):
        """Sets the contents of the y_t entry widget."""
        self.set_entry(self.en_y_t, text)

    def run(self):
        """Runs the amoebas in real time."""
        if self.running:
            # after_cancel

        self.running = True

        # find out how long to run
        end = self.en_end.get()
            self.end = float(eval(end))
            print 'End time must be a numeric expression.'

        self.start_time = time.time()
        self.after(0, self.step)

    def step(self):
        """Advance the Amoebas one step."""
        if not self.exists or not self.running:
        xexpr = self.en_x_t.get()
        yexpr = self.en_y_t.get()

        # see how much time has elapsed and evaluate x(t) and y(t)
        t = time.time() - self.start_time

        if t > self.end:

        x = eval(xexpr)
        y = eval(yexpr)
        print 't = %.1f   x = %.1f   y = %.1f' % (t, x, y)

        for amoeba in self.animals:
            amoeba.move(x, y)
        # schedule the next step
        self.after(self.delay, self.step)
    def clear(self):
        """Clears the amoebas and slime (but not the grid marks)."""
        for animal in self.animals:
class Amoeba(Animal):
    """A soft, round animal that lives in AmoebaWorld

        size: radius in hash marks
        color1 = color of the cell
        color2 = color of the nucleus
    def __init__(self, world=None):
        Animal.__init__(self, world)

        # size and color
        self.size = 0.2
        self.color1 = 'violet'
        self.color2 = 'medium orchid'
        self.tag = 'Amoeba%d' % id(self)

    def move(self, x, y):
        """Moves the amoeba and redraws."""
        self.x = x
        self.y = y

    def draw(self):
        """Draws the Amoeba."""

        # thetas is the sequence of angles used to compute the perimeter
        thetas = range(0, 360, 30)
        coords = self.poly_coords(self.x, self.y, thetas, self.size)

        slime = 'lavender'

        # draw the slime outline which will be left behind, fill=slime, outline=slime,
##        # draw the outer perimeter
##            fill=self.color1, outline=self.color2, tags=self.tag)
        # draw the perimeter of the nucleus
        coords = self.poly_coords(self.x, self.y, thetas, self.size/2),
            fill=self.color2, outline=self.color1, tags=self.tag)

    def poly_coords(self, x, y, thetas, size):
        """Computes coordinates of a polygon with random variation.

            x, y: center point
            thetas: sequence of angles
            size: minimum radius; actual radius is up to 2x bigger
        rs = [size+random.uniform(0, size) for theta in thetas]
        coords = [self.polar(x, y, r, theta) for (r, theta) in zip(rs, thetas)]
        return coords

if __name__ == '__main__':
    # create the GUI
    world = AmoebaWorld(interactive=True)

    # create the amoeba
    amoeba = Amoeba()
    # wait for the user to do something

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).