1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
| #!/usr/local/bin/python
# -*- coding:utf-8 -*-
from Tkinter import *
import threading
import time
from math import *
class ObjetTournant(threading.Thread):
def __init__(self, x, y, dans_canvas, ray, couleur, nom):
threading.Thread.__init__(self)
self.rayon = ray
self.x0 = x
self.y0 = y
self.coul = couleur
self.name = nom
self.angle = 0
self.c = dans_canvas
self.Fini = False
self.pause = False
self.reference = self.c.create_line(self.x0, self.y0, self.x0, self.y0+self.rayon, width = 8, fill=self.coul)
print "Initial :",self.c,"\n"
def run(self):
while not self.Fini:
if self.pause == False:
try:
new_x = self.x0 + self.rayon * sin (radians(self.angle))
new_y = self.y0 + self.rayon * cos (radians(self.angle))
print "Run : ",self.name,"\n"
self.c.coords(self.reference, self.x0, self.y0, int(new_x), int(new_y))
self.angle += 1
if self.angle >= 360:
self.angle = 0
except TclError:
break
time.sleep(0.01)
print "Le thread ",self.name," est fini.\n"
def arretetoi(self):
self.Fini = True
class Application:
def __init__(self):
self.root=Tk()
self.root.title('Objets tournants avec threads - v04')
self.root.geometry('700x200')
self.root.protocol("WM_DELETE_WINDOW", self.finirapplication)
self.ca = Canvas(self.root, bg="white")
self.ca.pack(side=LEFT, expand=YES, fill=BOTH)
self.fra = Frame(self.root)
self.but_1_1 = Button(self.fra, text="Start 1", command=self.objet1demarrer)
self.but_1_1.grid(row=0,column=0)
self.but_1_2 = Button(self.fra, text="Pause 1", state=DISABLED, command=self.objet1pause)
self.but_1_2.grid(row=0,column=1)
self.but_1_3 = Button(self.fra, text="Reprise 1", state=DISABLED, command=self.objet1pause)
self.but_1_3.grid(row=0,column=2)
self.but_1_4 = Button(self.fra, text="Stop 1", state=DISABLED, command=self.objet1stop)
self.but_1_4.grid(row=0,column=3)
self.but_2_1 = Button(self.fra, text="Start 2", command=self.objet2demarrer)
self.but_2_1.grid(row=1,column=0)
self.but_2_2 = Button(self.fra, text="Pause 2", state=DISABLED, command=self.objet2pause)
self.but_2_2.grid(row=1,column=1)
self.but_2_3 = Button(self.fra, text="Reprise 2", state=DISABLED, command=self.objet2pause)
self.but_2_3.grid(row=1,column=2)
self.but_2_4 = Button(self.fra, text="Stop 2", state=DISABLED, command=self.objet2stop)
self.but_2_4.grid(row=1,column=3)
self.but_3_1 = Button(self.fra, text="Start 3", command=self.objet3demarrer)
self.but_3_1.grid(row=2,column=0)
self.but_3_2 = Button(self.fra, text="Pause 3", state=DISABLED, command=self.objet3pause)
self.but_3_2.grid(row=2,column=1)
self.but_3_3 = Button(self.fra, text="Reprise 3", state=DISABLED, command=self.objet3pause)
self.but_3_3.grid(row=2,column=2)
self.but_3_4 = Button(self.fra, text="Stop 3", state=DISABLED, command=self.objet3stop)
self.but_3_4.grid(row=2,column=3)
self.but_4_1 = Button(self.fra, text="Start 4", command=self.objet4demarrer)
self.but_4_1.grid(row=3,column=0)
self.but_4_2 = Button(self.fra, text="Pause 4", state=DISABLED, command=self.objet4pause)
self.but_4_2.grid(row=3,column=1)
self.but_4_3 = Button(self.fra, text="Reprise 4", state=DISABLED, command=self.objet4pause)
self.but_4_3.grid(row=3,column=2)
self.but_4_4 = Button(self.fra, text="Stop 4", state=DISABLED, command=self.objet4stop)
self.but_4_4.grid(row=3,column=3)
self.fra.pack(side=RIGHT)
self.obj1 = ObjetTournant( 60,100,self.ca, 50, "red", "1")
self.obj2 = ObjetTournant(200,100,self.ca, 30, "green","2")
self.obj3 = ObjetTournant(300,100,self.ca, 40, "blue", "3")
self.obj4 = ObjetTournant(400,100,self.ca, 80, "black","4")
self.root.mainloop()
def finirapplication(self):
self.obj1.arretetoi()
sys.exit()
# ============================================================
def objet1demarrer(self):
print "On demarre le thread 1\n"
self.obj1.start()
self.but_1_1.config(state=DISABLED)
self.but_1_2.config(state=NORMAL)
self.but_1_4.config(state=NORMAL)
def objet1stop(self):
print "On doit stopper le thread 1\n"
self.obj1.arretetoi()
self.but_1_1.config(state=DISABLED)
self.but_1_2.config(state=DISABLED)
self.but_1_3.config(state=DISABLED)
self.but_1_4.config(state=DISABLED)
def objet1pause(self):
if self.obj1.pause == False:
print "On met le thread 1 en pause\n"
self.but_1_2.config(state=DISABLED)
self.but_1_3.config(state=NORMAL)
self.obj1.pause = True
else:
print "On reprend le thread 1 qui etait en pause\n"
self.but_1_2.config(state=NORMAL)
self.but_1_3.config(state=DISABLED)
self.obj1.pause = False
# ============================================================
def objet2demarrer(self):
print "On demarre le thread 2\n"
self.obj2.start()
self.but_2_1.config(state=DISABLED)
self.but_2_2.config(state=NORMAL)
self.but_2_4.config(state=NORMAL)
def objet2stop(self):
print "On doit stopper le thread 2\n"
self.obj2.arretetoi()
self.but_2_1.config(state=DISABLED)
self.but_2_2.config(state=DISABLED)
self.but_2_3.config(state=DISABLED)
self.but_2_4.config(state=DISABLED)
def objet2pause(self):
if self.obj2.pause == False:
print "On met le thread 2 en pause\n"
self.but_2_2.config(state=DISABLED)
self.but_2_3.config(state=NORMAL)
self.obj2.pause = True
else:
print "On reprend le thread 2 qui etait en pause\n"
self.but_2_2.config(state=NORMAL)
self.but_2_3.config(state=DISABLED)
self.obj2.pause = False
# ============================================================
def objet3demarrer(self):
print "On demarre le thread 3\n"
self.obj3.start()
self.but_3_1.config(state=DISABLED)
self.but_3_2.config(state=NORMAL)
self.but_3_4.config(state=NORMAL)
def objet3stop(self):
print "On doit stopper le thread 3\n"
self.obj3.arretetoi()
self.but_3_1.config(state=DISABLED)
self.but_3_2.config(state=DISABLED)
self.but_3_3.config(state=DISABLED)
self.but_3_4.config(state=DISABLED)
def objet3pause(self):
if self.obj3.pause == False:
print "On met le thread 3 en pause\n"
self.but_3_2.config(state=DISABLED)
self.but_3_3.config(state=NORMAL)
self.obj3.pause = True
else:
print "On reprend le thread 3 qui etait en pause\n"
self.but_3_2.config(state=NORMAL)
self.but_3_3.config(state=DISABLED)
self.obj3.pause = False
# ============================================================
def objet4demarrer(self):
print "On demarre le thread 4\n"
self.obj4.start()
self.but_4_1.config(state=DISABLED)
self.but_4_2.config(state=NORMAL)
self.but_4_4.config(state=NORMAL)
def objet4stop(self):
print "On doit stopper le thread 4\n"
self.obj4.arretetoi()
self.but_4_1.config(state=DISABLED)
self.but_4_2.config(state=DISABLED)
self.but_4_3.config(state=DISABLED)
self.but_4_4.config(state=DISABLED)
def objet4pause(self):
if self.obj4.pause == False:
print "On met le thread 4 en pause\n"
self.but_4_2.config(state=DISABLED)
self.but_4_3.config(state=NORMAL)
self.obj4.pause = True
else:
print "On reprend le thread 4 qui etait en pause\n"
self.but_4_2.config(state=NORMAL)
self.but_4_3.config(state=DISABLED)
self.obj4.pause = False
if __name__ == "__main__":
app = Application() |
Partager