1 | fromˆàTkinterˆàimportˆà* |
---|
2 | fromˆàmathˆàimportˆà* |
---|
3 | importˆàbacteria |
---|
4 | importˆàpetri |
---|
5 | importˆàrandom |
---|
6 | importˆàvector |
---|
7 | importˆàwebbrowser |
---|
8 | |
---|
9 | classˆàIF(object): |
---|
10 | ˆà ˆà defˆà__init__(self,delay=2,diam=300,num=0,env=1,reprod_a=False,reprod_s=False,life=0,stat=True): |
---|
11 | ˆà ˆà ˆà ˆà self.delay=delay |
---|
12 | ˆà ˆà ˆà ˆà self.diam=diam |
---|
13 | ˆà ˆà ˆà ˆà self.num=num |
---|
14 | ˆà ˆà ˆà ˆà self.env=env |
---|
15 | ˆà ˆà ˆà ˆà self.reprod_s=reprod_s |
---|
16 | ˆà ˆà ˆà ˆà self.reprod_a=reprod_a |
---|
17 | ˆà ˆà ˆà ˆà self.life=life |
---|
18 | ˆà ˆà ˆà ˆà self.stat=stat |
---|
19 | |
---|
20 | ˆà ˆà defˆà__repr__(self): |
---|
21 | ˆà ˆà ˆà ˆà returnˆà"<%s,%s,%s,%s,%s,%s,%s,%s>"%(self.delay,self.diam,self.num,\ |
---|
22 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆàself.env,self.reprod_a,\ |
---|
23 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆàself.reprod_s,self.life,self.stat) |
---|
24 | ˆà ˆà defˆàmutate(self): |
---|
25 | ˆà ˆà ˆà ˆà forˆàbact inˆàp.bacteria: |
---|
26 | ˆà ˆà ˆà ˆà ˆà ˆà ifˆànot(bact.mutated): |
---|
27 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà bact.mutated=True |
---|
28 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà p.total_mut+=1 |
---|
29 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà return |
---|
30 | |
---|
31 | ˆà ˆà defˆàsbmt(self): |
---|
32 | ˆà ˆà ˆà ˆà |
---|
33 | ˆà ˆà ˆà ˆà b=[] |
---|
34 | ˆà ˆà ˆà ˆà submit.config(text="Stop",command=self.stop) |
---|
35 | ˆà ˆà ˆà ˆà scl1.config(state=DISABLED) |
---|
36 | ˆà ˆà ˆà ˆà scl2.config(state=DISABLED) |
---|
37 | ˆà ˆà ˆà ˆà scl3.config(state=DISABLED) |
---|
38 | ˆà ˆà ˆà ˆà cb1.config(state=DISABLED) |
---|
39 | ˆà ˆà ˆà ˆà cb2.config(state=DISABLED) |
---|
40 | ˆà ˆà ˆà ˆà show_stat.config(state=DISABLED) |
---|
41 | ˆà ˆà ˆà ˆà self.num=scl1.get() |
---|
42 | ˆà ˆà ˆà ˆà self.diam=scl2.get() |
---|
43 | ˆà ˆà ˆà ˆà self.life=scl3.get() |
---|
44 | ˆà ˆà ˆà ˆà c.config(height=self.diam+20,width=self.diam+20,bg="gray75") |
---|
45 | ˆà ˆà ˆà ˆà filler=c.create_rectangle(0,0,self.diam+22,self.diam+22,fill="lightgrey") |
---|
46 | ˆà ˆà ˆà ˆà c.grid(row=0,column=2,rowspan=6,padx=50,pady=10) |
---|
47 | ˆà ˆà ˆà ˆà dish=c.create_oval(10,10,self.diam+10,self.diam+10,fill="white") |
---|
48 | ˆà ˆà ˆà ˆà forˆài inˆàrange(self.num): |
---|
49 | ˆà ˆà ˆà ˆà ˆà ˆà x_i=random.randint(-self.diam/2+15,self.diam/2-15) |
---|
50 | ˆà ˆà ˆà ˆà ˆà ˆà y_i=random.randint(15+(int)(-sqrt((self.diam/2)**2-x_i**2)),-15+(int)(sqrt((self.diam/2)**2-x_i**2))) |
---|
51 | ˆà ˆà ˆà ˆà ˆà ˆà b.append(bacteria.Bacteria(vector.Vector(x_i,y_i), |
---|
52 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà vector.Vector(0,1), |
---|
53 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.life,False)) |
---|
54 | ˆà ˆà ˆà ˆà forˆàbact inˆàb: |
---|
55 | ˆà ˆà ˆà ˆà ˆà ˆà bact.velocity =ˆàbact.rnd_velocity(pi/4*random.randrange(0,8)) |
---|
56 | ˆà ˆà ˆà ˆà p.num=self.num |
---|
57 | ˆà ˆà ˆà ˆà p.total=self.num |
---|
58 | ˆà ˆà ˆà ˆà p.total_max=self.num |
---|
59 | ˆà ˆà ˆà ˆà p.total_min=self.num |
---|
60 | ˆà ˆà ˆà ˆà p.total_mut=0 |
---|
61 | ˆà ˆà ˆà ˆà p.s_num=0 |
---|
62 | ˆà ˆà ˆà ˆà p.a_num=0 |
---|
63 | ˆà ˆà ˆà ˆà p.bacteria=b |
---|
64 | ˆà ˆà ˆà ˆà p.radius=self.diam/2 |
---|
65 | ˆà ˆà ˆà ˆà p.delta=5 |
---|
66 | ˆà ˆà ˆà ˆà p.bact_r=5 |
---|
67 | ˆà ˆà ˆà ˆà p.full_lifetime=self.life |
---|
68 | ˆà ˆà ˆà ˆà self.depict() |
---|
69 | |
---|
70 | ˆà ˆà defˆàpop_up(self): |
---|
71 | ˆà ˆà ˆà ˆà stat_fr=Toplevel() |
---|
72 | ˆà ˆà ˆà ˆà stat_fr.title("Statistics") |
---|
73 | ˆà ˆà ˆà ˆà init_stat=LabelFrame(stat_fr,text="Initial parameters") |
---|
74 | ˆà ˆà ˆà ˆà init_txt="Number of cells: %s\nDiameter of Petri dish: %s\nCell's lifetime: %s\n"%(self.num,self.diam, |
---|
75 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆàself.life) |
---|
76 | ˆà ˆà ˆà ˆà init_txt+="Asexual reproduction allowed: %s\nSexual reproduction allowed: %s"%(self.reprod_a,self.reprod_s) |
---|
77 | ˆà ˆà ˆà ˆà init_stat.grid(padx=15,pady=10) |
---|
78 | ˆà ˆà ˆà ˆà init_lbl =ˆàLabel(init_stat,text=init_txt) |
---|
79 | ˆà ˆà ˆà ˆà init_lbl.grid(padx=10) |
---|
80 | ˆà ˆà ˆà ˆà stat_frame=LabelFrame(stat_fr,text="Statistics") |
---|
81 | ˆà ˆà ˆà ˆà stat_frame.grid(padx=15,pady=10) |
---|
82 | ˆà ˆà ˆà ˆà stat_txt="Total number of cells: %s\nMaximum number of cells in dish: %s\n"%(p.total,p.total_max) |
---|
83 | ˆà ˆà ˆà ˆà stat_txt+="Minimum number of cells in dish: %s\nTotal number of mutated cells: %s\n"%(p.total_min,p.total_mut) |
---|
84 | ˆà ˆà ˆà ˆà stat_txt+="Number of asexual reproduction events: %s\nNumber of sexual reproduction events: %s"%(p.a_num,p.s_num) |
---|
85 | ˆà ˆà ˆà ˆà stat_lbl =ˆàLabel(stat_frame,text=stat_txt) |
---|
86 | ˆà ˆà ˆà ˆà stat_lbl.grid(padx=10) |
---|
87 | |
---|
88 | ˆà ˆà defˆàstop(self): |
---|
89 | ˆà ˆà ˆà ˆà c.after_cancel(self.rep) |
---|
90 | ˆà ˆà ˆà ˆà c.delete("all") |
---|
91 | ˆà ˆà ˆà ˆà dish=c.create_oval(10,10,self.diam+10,self.diam+10,fill="white")ˆà ˆà ˆà ˆà |
---|
92 | ˆà ˆà ˆà ˆà submit.config(text="Submit",command=self.sbmt) |
---|
93 | ˆà ˆà ˆà ˆà scl1.config(state=NORMAL) |
---|
94 | ˆà ˆà ˆà ˆà scl2.config(state=NORMAL) |
---|
95 | ˆà ˆà ˆà ˆà scl3.config(state=NORMAL) |
---|
96 | ˆà ˆà ˆà ˆà cb1.config(state=NORMAL) |
---|
97 | ˆà ˆà ˆà ˆà cb2.config(state=NORMAL) |
---|
98 | ˆà ˆà ˆà ˆà show_stat.config(state=NORMAL) |
---|
99 | |
---|
100 | ˆà ˆà defˆànew_reprod_a(self): |
---|
101 | ˆà ˆà ˆà ˆà self.reprod_a=not(self.reprod_a) |
---|
102 | ˆà ˆà ˆà ˆà return |
---|
103 | ˆà ˆà |
---|
104 | ˆà ˆà defˆànew_reprod_s(self): |
---|
105 | ˆà ˆà ˆà ˆà self.reprod_s=not(self.reprod_s) |
---|
106 | ˆà ˆà ˆà ˆà return |
---|
107 | |
---|
108 | ˆà ˆà defˆàhelp(self): |
---|
109 | ˆà ˆà ˆà ˆà url =ˆà'About.html' |
---|
110 | ˆà ˆà ˆà ˆà webbrowser.open(url,ˆànew=1,ˆàautoraise=True) |
---|
111 | |
---|
112 | ˆà ˆà defˆàdepict(self): |
---|
113 | ˆà ˆà ˆà ˆà c.delete("all") |
---|
114 | ˆà ˆà ˆà ˆà dish=c.create_oval(10,10,self.diam+10,self.diam+10,fill="white") |
---|
115 | ˆà ˆà ˆà ˆà env=scl.get() |
---|
116 | ˆà ˆà ˆà ˆà ifˆàp.step(0.5,int(env),self.reprod_a,self.reprod_s): |
---|
117 | ˆà ˆà ˆà ˆà ˆà ˆà forˆàbact inˆàp.bacteria: |
---|
118 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆàbact.mutated: |
---|
119 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà color="black" |
---|
120 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà else: |
---|
121 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà color="white" |
---|
122 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà c.create_oval(10+self.diam/2+bact.position.x-p.bact_r,10+self.diam/2+bact.position.y-p.bact_r,\ |
---|
123 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà 10+self.diam/2+bact.position.x+p.bact_r*2,\ |
---|
124 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà 10+self.diam/2+bact.position.y+p.bact_r*2,fill=color) |
---|
125 | ˆà ˆà ˆà ˆà ˆà ˆà self.rep=c.after(self.delay,self.depict) |
---|
126 | ˆà ˆà ˆà ˆà else: |
---|
127 | ˆà ˆà ˆà ˆà ˆà ˆà self.stop() |
---|
128 | |
---|
129 | ˆà ˆà defˆàupdate_scale(widget,ˆàtimes): |
---|
130 | ˆà ˆà ˆà ˆà value =ˆàwidget.get() |
---|
131 | ˆà ˆà ˆà ˆà length =ˆàwidget['to']ˆà-ˆàwidget['from'] |
---|
132 | ˆà ˆà ˆà ˆà value +=ˆàlength *ˆàtimes /ˆà10 |
---|
133 | ˆà ˆà ˆà ˆà widget.set(value) |
---|
134 | |
---|
135 | root =ˆàTk() |
---|
136 | root.title("Interface") |
---|
137 | intf=IF() |
---|
138 | c=Canvas() |
---|
139 | p=petri.Petri() |
---|
140 | b=[] |
---|
141 | |
---|
142 | root.bind_class("Scale",ˆà"<4>",ˆàlambdaˆàev:ˆàupdate_scale(ev.widget,ˆà+1)) |
---|
143 | root.bind_class("Scale",ˆà"<5>",ˆàlambdaˆàev:ˆàupdate_scale(ev.widget,ˆà-1)) |
---|
144 | |
---|
145 | menubar =ˆàMenu(root) |
---|
146 | |
---|
147 | # create a pulldown menu, and add it to the menu bar |
---|
148 | controlsmenu =ˆàMenu(menubar,ˆàtearoff=0) |
---|
149 | controlsmenu.add_command(label="Start",ˆàcommand=intf.sbmt) |
---|
150 | controlsmenu.add_command(label="Stop",ˆàcommand=intf.stop) |
---|
151 | controlsmenu.add_command(label="Add mutation",ˆàcommand=intf.mutate) |
---|
152 | controlsmenu.add_separator() |
---|
153 | controlsmenu.add_command(label="Exit",ˆàcommand=root.destroy) |
---|
154 | menubar.add_cascade(label="Controls",ˆàmenu=controlsmenu) |
---|
155 | |
---|
156 | helpmenu =ˆàMenu(menubar,ˆàtearoff=0) |
---|
157 | helpmenu.add_command(label="About",ˆàcommand=intf.help) |
---|
158 | menubar.add_cascade(label="Help",ˆàmenu=helpmenu) |
---|
159 | |
---|
160 | # display the menu |
---|
161 | root.config(menu=menubar) |
---|
162 | |
---|
163 | |
---|
164 | |
---|
165 | init_frame=LabelFrame(root,text="Initial parameters") |
---|
166 | init_frame.grid(padx=15,pady=10) |
---|
167 | scl1 =ˆàScale(init_frame,orient=HORIZONTAL,length=100,from_=1,\ |
---|
168 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà to=10,sliderlength=7,width=10) |
---|
169 | scl1.grid(row=0,ˆàcolumn=0,padx=10,pady=5) |
---|
170 | lbl1 =ˆàLabel(init_frame,text="number of cells",bg="white") |
---|
171 | lbl1.grid(row=0,column=1,padx=10) |
---|
172 | scl2 =ˆàScale(init_frame,orient=HORIZONTAL,length=100,from_=100,\ |
---|
173 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà to=500,sliderlength=7,width=10) |
---|
174 | scl2.grid(row=1,ˆàcolumn=0,padx=10,pady=5) |
---|
175 | lbl2 =ˆàLabel(init_frame,text="diameter of Petri dish",bg="white") |
---|
176 | lbl2.grid(row=1,column=1,padx=10) |
---|
177 | scl3 =ˆàScale(init_frame,orient=HORIZONTAL,length=100,from_=1,\ |
---|
178 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà to=1000,sliderlength=7,width=10) |
---|
179 | scl3.grid(row=2,ˆàcolumn=0,padx=10,pady=5) |
---|
180 | lbl3 =ˆàLabel(init_frame,text="cells' lifetime",bg="white") |
---|
181 | lbl3.grid(row=2,column=1,padx=10) |
---|
182 | lbl_frame=LabelFrame(init_frame,text="Reproduction") |
---|
183 | lbl_frame.grid(row=4,column=0,columnspan=2,padx=15,pady=10) |
---|
184 | list=["Asexual","Sexual"] |
---|
185 | cb1 =ˆàCheckbutton(lbl_frame,text="Asexual",command=intf.new_reprod_a) |
---|
186 | cb1.pack() |
---|
187 | cb2 =ˆàCheckbutton(lbl_frame,text="Sexual",command=intf.new_reprod_s) |
---|
188 | cb2.pack() |
---|
189 | |
---|
190 | submit=Button(root,text="Submit",width=25,command=intf.sbmt) |
---|
191 | submit.grid(row=1,column=0,columnspan=2,pady=10) |
---|
192 | |
---|
193 | chng_frame=LabelFrame(root,text="Changeable parameters") |
---|
194 | chng_frame.grid(padx=15,pady=10) |
---|
195 | scl =ˆàScale(chng_frame,orient=HORIZONTAL,length=100,from_=1,\ |
---|
196 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà to=100,sliderlength=7,width=10) |
---|
197 | scl.grid(row=0,ˆàcolumn=0,padx=10,pady=5) |
---|
198 | lbl =ˆàLabel(chng_frame,text="environment",bg="white") |
---|
199 | lbl.grid(row=0,column=1,padx=10) |
---|
200 | btn =ˆàButton(chng_frame,text="Add mutation",width=25,command=intf.mutate) |
---|
201 | btn.grid(row=1,column=0,columnspan=2,pady=10) |
---|
202 | |
---|
203 | show_stat =ˆàButton(root,text="Show statistics",width=25,command=intf.pop_up) |
---|
204 | show_stat.grid(pady=10,padx=10) |
---|
205 | |
---|
206 | #root.after(intf.delay,intf.depict()) |
---|
207 | |
---|
208 | mainloop() |
---|