Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.cmm.msu.su/trac/petri_dish/browser/scale.py?format=txt
Дата изменения: Thu Dec 23 16:58:48 2010
Дата индексирования: Tue Oct 2 11:36:39 2012
Кодировка:
from Tkinter import *
from math import *
import bacteria
import petri
import random
import vector
import webbrowser

class IF(object):
def __init__(self,delay=2,diam=300,num=0,env=1,reprod_a=False,reprod_s=False,life=0,stat=True):
self.delay=delay
self.diam=diam
self.num=num
self.env=env
self.reprod_s=reprod_s
self.reprod_a=reprod_a
self.life=life
self.stat=stat

def __repr__(self):
return "<%s,%s,%s,%s,%s,%s,%s,%s>"%(self.delay,self.diam,self.num,\
self.env,self.reprod_a,\
self.reprod_s,self.life,self.stat)
def mutate(self):
for bact in p.bacteria:
if not(bact.mutated):
bact.mutated=True
p.total_mut+=1
return

def sbmt(self):

b=[]
submit.config(text="Stop",command=self.stop)
scl1.config(state=DISABLED)
scl2.config(state=DISABLED)
scl3.config(state=DISABLED)
cb1.config(state=DISABLED)
cb2.config(state=DISABLED)
show_stat.config(state=DISABLED)
self.num=scl1.get()
self.diam=scl2.get()
self.life=scl3.get()
c.config(height=self.diam+20,width=self.diam+20,bg="gray75")
filler=c.create_rectangle(0,0,self.diam+22,self.diam+22,fill="lightgrey")
c.grid(row=0,column=2,rowspan=6,padx=50,pady=10)
dish=c.create_oval(10,10,self.diam+10,self.diam+10,fill="white")
for i in range(self.num):
x_i=random.randint(-self.diam/2+15,self.diam/2-15)
y_i=random.randint(15+(int)(-sqrt((self.diam/2)**2-x_i**2)),-15+(int)(sqrt((self.diam/2)**2-x_i**2)))
b.append(bacteria.Bacteria(vector.Vector(x_i,y_i),
vector.Vector(0,1),
self.life,False))
for bact in b:
bact.velocity = bact.rnd_velocity(pi/4*random.randrange(0,8))
p.num=self.num
p.total=self.num
p.total_max=self.num
p.total_min=self.num
p.total_mut=0
p.s_num=0
p.a_num=0
p.bacteria=b
p.radius=self.diam/2
p.delta=5
p.bact_r=5
p.full_lifetime=self.life
self.depict()

def pop_up(self):
stat_fr=Toplevel()
stat_fr.title("Statistics")
init_stat=LabelFrame(stat_fr,text="Initial parameters")
init_txt="Number of cells: %s\nDiameter of Petri dish: %s\nCell's lifetime: %s\n"%(self.num,self.diam,
self.life)
init_txt+="Asexual reproduction allowed: %s\nSexual reproduction allowed: %s"%(self.reprod_a,self.reprod_s)
init_stat.grid(padx=15,pady=10)
init_lbl = Label(init_stat,text=init_txt)
init_lbl.grid(padx=10)
stat_frame=LabelFrame(stat_fr,text="Statistics")
stat_frame.grid(padx=15,pady=10)
stat_txt="Total number of cells: %s\nMaximum number of cells in dish: %s\n"%(p.total,p.total_max)
stat_txt+="Minimum number of cells in dish: %s\nTotal number of mutated cells: %s\n"%(p.total_min,p.total_mut)
stat_txt+="Number of asexual reproduction events: %s\nNumber of sexual reproduction events: %s"%(p.a_num,p.s_num)
stat_lbl = Label(stat_frame,text=stat_txt)
stat_lbl.grid(padx=10)

def stop(self):
c.after_cancel(self.rep)
c.delete("all")
dish=c.create_oval(10,10,self.diam+10,self.diam+10,fill="white")
submit.config(text="Submit",command=self.sbmt)
scl1.config(state=NORMAL)
scl2.config(state=NORMAL)
scl3.config(state=NORMAL)
cb1.config(state=NORMAL)
cb2.config(state=NORMAL)
show_stat.config(state=NORMAL)

def new_reprod_a(self):
self.reprod_a=not(self.reprod_a)
return

def new_reprod_s(self):
self.reprod_s=not(self.reprod_s)
return

def help(self):
url = 'About.html'
webbrowser.open(url, new=1, autoraise=True)

def depict(self):
c.delete("all")
dish=c.create_oval(10,10,self.diam+10,self.diam+10,fill="white")
env=scl.get()
if p.step(0.5,int(env),self.reprod_a,self.reprod_s):
for bact in p.bacteria:
if bact.mutated:
color="black"
else:
color="white"
c.create_oval(10+self.diam/2+bact.position.x-p.bact_r,10+self.diam/2+bact.position.y-p.bact_r,\
10+self.diam/2+bact.position.x+p.bact_r*2,\
10+self.diam/2+bact.position.y+p.bact_r*2,fill=color)
self.rep=c.after(self.delay,self.depict)
else:
self.stop()

def update_scale(widget, times):
value = widget.get()
length = widget['to'] - widget['from']
value += length * times / 10
widget.set(value)

root = Tk()
root.title("Interface")
intf=IF()
c=Canvas()
p=petri.Petri()
b=[]

root.bind_class("Scale", "<4>", lambda ev: update_scale(ev.widget, +1))
root.bind_class("Scale", "<5>", lambda ev: update_scale(ev.widget, -1))

menubar = Menu(root)

# create a pulldown menu, and add it to the menu bar
controlsmenu = Menu(menubar, tearoff=0)
controlsmenu.add_command(label="Start", command=intf.sbmt)
controlsmenu.add_command(label="Stop", command=intf.stop)
controlsmenu.add_command(label="Add mutation", command=intf.mutate)
controlsmenu.add_separator()
controlsmenu.add_command(label="Exit", command=root.destroy)
menubar.add_cascade(label="Controls", menu=controlsmenu)

helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="About", command=intf.help)
menubar.add_cascade(label="Help", menu=helpmenu)

# display the menu
root.config(menu=menubar)



init_frame=LabelFrame(root,text="Initial parameters")
init_frame.grid(padx=15,pady=10)
scl1 = Scale(init_frame,orient=HORIZONTAL,length=100,from_=1,\
to=10,sliderlength=7,width=10)
scl1.grid(row=0, column=0,padx=10,pady=5)
lbl1 = Label(init_frame,text="number of cells",bg="white")
lbl1.grid(row=0,column=1,padx=10)
scl2 = Scale(init_frame,orient=HORIZONTAL,length=100,from_=100,\
to=500,sliderlength=7,width=10)
scl2.grid(row=1, column=0,padx=10,pady=5)
lbl2 = Label(init_frame,text="diameter of Petri dish",bg="white")
lbl2.grid(row=1,column=1,padx=10)
scl3 = Scale(init_frame,orient=HORIZONTAL,length=100,from_=1,\
to=1000,sliderlength=7,width=10)
scl3.grid(row=2, column=0,padx=10,pady=5)
lbl3 = Label(init_frame,text="cells' lifetime",bg="white")
lbl3.grid(row=2,column=1,padx=10)
lbl_frame=LabelFrame(init_frame,text="Reproduction")
lbl_frame.grid(row=4,column=0,columnspan=2,padx=15,pady=10)
list=["Asexual","Sexual"]
cb1 = Checkbutton(lbl_frame,text="Asexual",command=intf.new_reprod_a)
cb1.pack()
cb2 = Checkbutton(lbl_frame,text="Sexual",command=intf.new_reprod_s)
cb2.pack()

submit=Button(root,text="Submit",width=25,command=intf.sbmt)
submit.grid(row=1,column=0,columnspan=2,pady=10)

chng_frame=LabelFrame(root,text="Changeable parameters")
chng_frame.grid(padx=15,pady=10)
scl = Scale(chng_frame,orient=HORIZONTAL,length=100,from_=1,\
to=100,sliderlength=7,width=10)
scl.grid(row=0, column=0,padx=10,pady=5)
lbl = Label(chng_frame,text="environment",bg="white")
lbl.grid(row=0,column=1,padx=10)
btn = Button(chng_frame,text="Add mutation",width=25,command=intf.mutate)
btn.grid(row=1,column=0,columnspan=2,pady=10)

show_stat = Button(root,text="Show statistics",width=25,command=intf.pop_up)
show_stat.grid(pady=10,padx=10)

#root.after(intf.delay,intf.depict())

mainloop()