Changeset 20:2df48c61bd42
- Timestamp:
- 12/20/10 01:44:18 (5 years ago)
- Branch:
- default
- Files:
-
- 2 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
scale.py
r18 r20 ˆà 1 1 from Tkinter import *ˆà 2 ˆà import Bacteriaˆà 3 ˆà import Petriˆà ˆà 2 from math import *ˆà ˆà 3 import bacteriaˆà ˆà 4 import petriˆà ˆà 5 import randomˆà ˆà 6 import vectorˆà 4 7 ˆà 5 8 class IF(object):ˆà 6 ˆà def __init__(self,delay= 10,diam=300,num=1,env=1,reprod_a=False,reprod_s=False,life=1):ˆàˆà 9 def __init__(self,delay=2,diam=300,num=1,env=1,reprod_a=False,reprod_s=False,life=1):ˆà 7 10 self.delay=delayˆà 8 11 self.diam=diamˆà òÀæ òÀæ ˆà 17 20 self.env,self.reprod_a,\ˆà 18 21 self.reprod_s,self.life)ˆà 19 ˆà def mutate():ˆà 20 ˆà passˆà ˆà 22 def mutate(self):ˆà ˆà 23 for bact in p.bacteria:ˆà ˆà 24 if not(bact.mutated):ˆà ˆà 25 bact.mutated=Trueˆà ˆà 26 returnˆà 21 27 ˆà 22 28 def sbmt(self):ˆà ˆà 29 b=[]ˆà 23 30 submit.config(text="Stop",command=self.stop)ˆà 24 31 scl1.config(state=DISABLED)ˆà òÀæ òÀæ ˆà 34 41 c.grid(row=0,column=2,rowspan=6,padx=50,pady=10)ˆà 35 42 dish=c.create_oval(10,10,self.diam+10,self.diam+10,fill="white")ˆà 36 ˆà print selfˆà ˆà 43 for i in range(self.num):ˆà ˆà 44 x_i=random.randint(-self.diam/2+15,self.diam/2-15)ˆà ˆà 45 y_i=random.randint(15+(int)(-sqrt((self.diam/2)**2-x_i**2)),-15+(int)(sqrt((self.diam/2)**2-x_i**2)))ˆà ˆà 46 b.append(bacteria.Bacteria(vector.Vector(x_i,y_i),ˆà ˆà 47 vector.Vector(0,1),ˆà ˆà 48 self.life,False))ˆà ˆà 49 for bact in b:ˆà ˆà 50 bact.velocity = bact.rnd_velocity(pi/4*random.randrange(0,8))ˆà ˆà 51 p.num=self.numˆà ˆà 52 p.bacteria=bˆà ˆà 53 p.radius=self.diam/2ˆà ˆà 54 p.delta=5ˆà ˆà 55 p.bact_r=5ˆà ˆà 56 p.full_lifetime=self.lifeˆà ˆà 57 self.depict()ˆà 37 58 ˆà 38 59 def stop(self):ˆà ˆà 60 c.after_cancel(self.rep)ˆà ˆà 61 c.delete("all")ˆà 39 62 submit.config(text="Submit",command=self.sbmt)ˆà 40 63 scl1.config(state=NORMAL)ˆà òÀæ òÀæ ˆà 59 82 ˆà 60 83 def depict(self):ˆà 61 ˆà passˆà ˆà 84 c.delete("all")ˆà ˆà 85 dish=c.create_oval(10,10,self.diam+10,self.diam+10,fill="white")ˆà ˆà 86 env=scl.get()ˆà ˆà 87 p.step(0.5,int(env),self.reprod_a,self.reprod_s)ˆà ˆà 88 for bact in p.bacteria:ˆà ˆà 89 if bact.mutated:ˆà ˆà 90 color="black"ˆà ˆà 91 else:ˆà ˆà 92 color="white"ˆà ˆà 93 c.create_oval(10+self.diam/2+bact.position.x-p.bact_r,10+self.diam/2+bact.position.y-p.bact_r,\ˆà ˆà 94 10+self.diam/2+bact.position.x+p.bact_r*2,\ˆà ˆà 95 10+self.diam/2+bact.position.y+p.bact_r*2,fill=color)ˆà ˆà 96 self.rep=c.after(self.delay,self.depict)ˆà 62 97 ˆà 63 ˆà def update_scale(widget, times):ˆà64 ˆà value = widget.get()ˆà65 ˆà length = widget['to'] - widget['from']ˆà66 ˆà value += length * times / 10ˆà67 ˆà widget.set(value)ˆàˆà 98 def update_scale(widget, times):ˆà ˆà 99 value = widget.get()ˆà ˆà 100 length = widget['to'] - widget['from']ˆà ˆà 101 value += length * times / 10ˆà ˆà 102 widget.set(value)ˆà 68 103 ˆà 69 104 root = Tk()ˆà òÀæ òÀæ ˆà 71 106 intf=IF()ˆà 72 107 c=Canvas()ˆà ˆà 108 p=petri.Petri()ˆà ˆà 109 b=[]ˆà 73 110 ˆà 74 111 root.bind_class("Scale", "<4>", lambda ev: update_scale(ev.widget, +1))ˆà òÀæ òÀæ ˆà 99 136 init_frame.grid(padx=15,pady=10)ˆà 100 137 scl1 = Scale(init_frame,orient=HORIZONTAL,length=100,from_=1,\ˆà 101 ˆà to= 50,sliderlength=7,width=10)ˆàˆà 138 to=10,sliderlength=7,width=10)ˆà 102 139 scl1.grid(row=0, column=0,padx=10,pady=5)ˆà 103 140 lbl1 = Label(init_frame,text="number of cells",bg="white")ˆà òÀæ òÀæ ˆà 109 146 lbl2.grid(row=1,column=1,padx=10)ˆà 110 147 scl3 = Scale(init_frame,orient=HORIZONTAL,length=100,from_=1,\ˆà 111 ˆà to= 30,sliderlength=7,width=10)ˆàˆà 148 to=1000,sliderlength=7,width=10)ˆà 112 149 scl3.grid(row=2, column=0,padx=10,pady=5)ˆà 113 150 lbl3 = Label(init_frame,text="cells' lifetime",bg="white")ˆà
Note: See TracChangeset
for help on using the changeset viewer.