[tkinter Example] Conway

Post here if you need help with creating a Graphical User Interface in Python.

[tkinter Example] Conway

Postby Mekire » Tue Mar 19, 2013 12:27 pm

I wrote this up a couple weeks ago when someone asked about this, but the person never responded to queries for effort so I never posted. This is just a simple non-interactive Conway demo with torus wraparound. I never really work with tkinter so this was a bit of an experiment; if anyone has any advice feel free to post it.

Rule settings, cell size, and framerate can be edited easily within the code:
Code: Select all
try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

#Declare cellular automata rules here.
BIRTH = (3,) #Neighbors an empty cell needs to be born.
SURVIVE = (2,3) #Neighbors a living cell needs to survive.
#Seed for a 'Gosper Glider Gun' in set form.
SEED = {(22, 3),(17, 5),(16, 8),( 2, 6),(35, 3),(16, 4),(36, 4),(14, 3),(25, 7),
        (22, 4),(21, 4),(18, 6),( 1, 6),(25, 1),(36, 3),(13, 9),( 2, 5),(35, 4),
        (14, 9),(17, 7),(11, 7),(17, 6),(13, 3),(11, 5),(25, 6),(23, 2),(21, 3),
        ( 1, 5),(15, 6),(12, 4),(21, 5),(25, 2),(22, 5),(23, 6),(11, 6),(12, 8)}
#Convenient constant for looking at adjacent neighbors.
ADJACENTS = {(-1, 1),(0, 1),(1,1 ),(-1, 0),(1, 0),(-1,-1),(0,-1),(1,-1)}

class Life:
    def __init__(self):
        self.live = SEED.copy() #Set of all living cells.
        self.pop = len(self.live) #Population counter.
        self.gen = 0   #Generation counter.
        self.speed = 150 #FPS of animation
        self.sz = 5 #Size of a cell.
        self.w = int(MyCanvas.cget("width"))//self.sz  #Canvas width in cells.
        self.h = int(MyCanvas.cget("height"))//self.sz #Canvas height in cells.
    def draw(self):
        for x,y in self.live:
            rect = (x*self.sz,y*self.sz,(x+1)*self.sz,(y+1)*self.sz)
##            MyCanvas.create_oval(rect,outline="black")
##            MyCanvas.create_oval(rect,outline="yellow",fill="blue")
##            MyCanvas.create_rectangle(rect,outline="green",fill="purple",width=2)
            MyCanvas.create_rectangle(rect,fill="orange")
    def stats(self):
        gen = "Generation: {}".format(self.gen)
        pop = "Population: {}".format(self.pop)
        MyCanvas.create_text((700,20),text=gen,fill="red",anchor="w")
        MyCanvas.create_text((700,35),text=pop,fill="red",anchor="w")
    def frame(self):
        self.process()
        MyCanvas.delete(tk.ALL)
        self.draw()
        self.stats()
        root.after(1000//self.speed,self.frame)
    def process(self):
        #Count everyone's neighbors.
        neighbors = {}
        for x,y in self.live:
            for i,j in ADJACENTS:
                check = ((x+i)%self.w,(y+j)%self.h) #Wrap-around.
                neighbors[check] = neighbors.get(check,0)+1
        #Make changes accordingly.
        next_liv = set()
        for guy in neighbors:
            if neighbors[guy] in BIRTH and guy not in self.live:
                next_liv |= set([guy])
            elif neighbors[guy] in SURVIVE and guy in self.live:
                next_liv |= set([guy])
        self.live = next_liv
        self.pop = len(self.live)
        self.gen += 1

#########################################
if __name__ == "__main__":
    root = tk.Tk()
    root.title("Conway's Game of Life")
    MyCanvas = tk.Canvas(root,width=800,height=600,highlightthickness=0,bd=0,bg='grey')
    MyCanvas.pack()
    MyLife = Life()
    MyLife.frame()
    root.mainloop()
Should run with Python 2 and 3 (feel free to throw this in scripts if anyone feels it would fit better there).

Cheers,
-Mek
Last edited by Yoriz on Tue Mar 19, 2013 1:01 pm, edited 1 time in total.
Reason: Added Example to the title
User avatar
Mekire
 
Posts: 811
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [tkinter Example] Conway

Postby tubos » Fri Mar 29, 2013 4:50 pm

Thx for the example
tubos
 
Posts: 2
Joined: Fri Mar 29, 2013 4:48 pm


Return to GUI

Who is online

Users browsing this forum: No registered users and 2 guests