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
| import tkinter as tk
DIM_GRILLE = (20, 20)
DIM_CASEL = 60
DIM_CASEH = 30
DIM_LIGNE = 1
DIM_CANEVAS = (
DIM_GRILLE[0]*DIM_CASEL + (DIM_GRILLE[0]+1)*DIM_LIGNE,
DIM_GRILLE[1]*DIM_CASEH + (DIM_GRILLE[1]+1)*DIM_LIGNE
)
def destroyer2(evt) :
#r = can.find_overlapping(evt.x, evt.y, evt.x, evt.y)
r = can.find_enclosed(evt.x, evt.y, evt.x, evt.y)
print("R= "+str(r))
print(can.type(r[0]))
print(can.type(r[1]))
def destroyer(evt) :
#print("SELECT = "+str(can.select_item()))
#print(can.find_all())
#print("CLOSEST = "+str(can.find_closest(evt.x, evt.y, evt.x, evt.y)))
#print("CLOSEST X = "+str(can.find_closest(evt.x, evt.y)))
#print("ENCLOSED X = "+str(can.find_enclosed(evt.x, evt.y, evt.x, evt.y)))
r = can.find_overlapping(evt.x, evt.y, evt.x, evt.y)
#print("R= "+str(r))
myTupL = len(r)
print("LONGUEUR = "+str(myTupL))
if myTupL == 2 :
rid = r[0]
rid2 = r[1]
print("RID = "+str(rid))
print("RID2 = "+str(rid2))
#print("R = "+str(r))
myColor = can.itemcget(rid,'fill')
myText = can.itemcget(rid2,'text')
else:
print("FALSE")
return
print(myColor)
#print(rid2)
if not rid :
print("ARRETEZ TOUT")
if not rid2 >1 :
print("STOP1 = "+ str(rid2))
#return
#if myColor == "yellow":
elif myTupL==2 and (myText == "VRAI" or myColor == "yellow"):
# Récupération des index ligne et colonne contenant l'id rectangle
for il, ligne in enumerate(items) :
#print("IL = "+str(il))
if rid in ligne:
ic = ligne.index(rid)
#print("IC2 = "+str(ic2))
break
#print("IC2 = "+str(ic))
# Suppressions de la ligne et colonne du tableau items1
# Et suppression dans le canevas
lid = items.pop(il)
for oid in lid:
can.delete(oid)
for ligne in items :
oid = ligne.pop(ic)
can.delete(oid)
if not any(items) :
# Plus rien à faire, le tableau d'items1 est vide.
return
''
# Récupération des index ligne et colonne contenant l'id rectangle
for il, ligne in enumerate(items2) :
#print("IL = "+str(il))
if rid2 in ligne:
ic = ligne.index(rid2)
break
# Suppressions de la ligne et colonne du tableau items1
# Et suppression dans le canevas
lid = items2.pop(il)
for oid in lid:
can.delete(oid)
for ligne in items2 :
oid = ligne.pop(ic)
can.delete(oid)
if not any(items2) :
# Plus rien à faire, le tableau d'items1 est vide.
return
''
nombreLignes = len(items)
nombreColonnes = len(items[0])
# Calculs des dimensions de chaque lignes
# Soustraction des lignes de la hauteur du canevas
hautCan = DIM_CANEVAS[1] - (nombreLignes+1) * DIM_LIGNE
# Répartition de la hauteur
hauteurs = [hautCan // nombreLignes] * nombreLignes
for i in range(hautCan % nombreLignes) :
hauteurs[i] += 1
# Calculs des dimensions de chaque colonnes
largCan = DIM_CANEVAS[0] - (nombreColonnes+1) * DIM_LIGNE
largeurs = [largCan // nombreColonnes] * nombreColonnes
for i in range(largCan % nombreColonnes) :
largeurs[i] += 1
''
# Mises à jour des dimensions RECTANGLE
ih = 0
y = DIM_LIGNE
for ligne in items :
il = 0
x = DIM_LIGNE
for oid in ligne :
can.coords(oid, x, y, x+largeurs[il], y+hauteurs[ih])
x += largeurs[il] + DIM_LIGNE
il += 1
ih += 1
try :
y += hauteurs[ih] + DIM_LIGNE
except IndexError :
# Dernier tour de boucle
break
''
# Mises à jour des dimensions TEXTE
ih = 0
y = DIM_LIGNE
for ligne in items2 :
il = 0
x = DIM_LIGNE
for oid in ligne :
can.coords(oid, x+largeurs[il]/2, y+hauteurs[ih]/2)
x += largeurs[il] + DIM_LIGNE
il += 1
ih += 1
try :
y += hauteurs[ih] + DIM_LIGNE
except IndexError :
# Dernier tour de boucle
break
''
j=evt.x#//DIM_CASEL)
i=evt.y#//DIM_CASEL)
#print('Les coordonnées du carreau cliqué sont ('+str(i)+','+str(j)+')')
else:
pass
fenetre = tk.Tk()
can = tk.Canvas(
fenetre,
width=DIM_CANEVAS[0],
height=DIM_CANEVAS[1],
bg='black',
highlightthickness=0,
)
can.grid()
items = []
items2 = []
colors = ['white', 'yellow']
y = DIM_LIGNE
for ligne in range(DIM_GRILLE[1]) :
x = DIM_LIGNE
ids1 = []
ids2 = []
colors.reverse()
for col in range(DIM_GRILLE[0]) :
#text_item = can.create_text(x+DIM_CASEL, y+DIM_CASEL, text="VRAI", fill="white")
if ligne == 0 and col==0:
text_item = can.create_text(x+DIM_CASEL/2, y+DIM_CASEH/2, text=" ", fill="black")
bbox = can.bbox(text_item)
i = can.create_rectangle(bbox, outline="black", fill="red")
can.tag_raise(text_item,i)
elif ligne == 0 and col>0:
text_item = can.create_text(x+DIM_CASEL/2, y+DIM_CASEH/2, text=" ", fill="black")
bbox = can.bbox(text_item)
i = can.create_rectangle(bbox, outline="black", fill="red")
can.tag_raise(text_item,i)
elif col == 0 and ligne>0:
text_item = can.create_text(x+DIM_CASEL/2, y+DIM_CASEH/2, text=" ", fill="black")
bbox = can.bbox(text_item)
i = can.create_rectangle(bbox, outline="black", fill="red")
can.tag_raise(text_item,i)
else:
text_item = can.create_text(x+DIM_CASEL/2, y+DIM_CASEH/2, text="VRAI", fill="black")
bbox = can.bbox(text_item)
i = can.create_rectangle(bbox, outline="red", fill=colors[col % 2])
can.tag_raise(text_item,i)
ids1.append(i)
ids2.append(text_item)
#print(i)
x += DIM_CASEL + DIM_LIGNE
items.append(ids1)
items2.append(ids2)
y += DIM_CASEH + DIM_LIGNE
can.bind('<Button-1>', destroyer)
fenetre.mainloop() |
Partager