#!/usr/bin/python
#http://mail.python.org/pipermail/tutor/2005-June/039329.html
from Tkinter import *
from time import *
import random
w=5
h=6
c=['red','blue','green','yellow','orange']
mtc=[]
for z in range(w*h):
	mtc.append(0)
a=[]
def color():
	for z in range(w*h):
		a.append(z)
	random.shuffle(a)
	for z in range(w*h):
		a[z]=c[a[z]%5]
color()
def make(ind):
	def crd():
		global clk
		global p
		clk+=1
		if clk%2==1:
			global one
			one=a[ind]
			mtc[ind]=1
			p=ind
			setup(w, h)
		else:
			global two
			two=a[ind]
			mtc[ind]=1
			setup(w, h)
			if one==two:
				mtc[p]=2
				mtc[ind]=2
			else:
				root.update_idletasks()				
				mtc[p]=0
				mtc[ind]=0
				sleep(.5)
				setup(w, h)
	return crd
	return ind
clk=0
root = Tk()
def setup(w, h):
	crw=0
	ccl=0
	for z in range(w*h):
		ind=z
		ste='normal'
		col='SystemButtonFace'
		maker = make(ind)
		if ind%w!=0:
			ccl+=1
		if ind%w==0:
			crw+=1
			ccl=0
		if mtc[z]==1 or mtc[z]==2:
			col=a[z]
			ste='disabled'
		b = Button(root, bg=col, state=ste, text='    ', command=maker).grid(row=crw, column=ccl)

setup(w, h)
mainloop()
