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
| import tkinter as tk
DIM_GRILLE = (20, 20)
DIM_CASEL = 30
DIM_CASEH = 20
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 destroyer(evt) :
#print(F[2])
print (can.itemcget(items[0], 'fill'))
r = can.find_overlapping(evt.x, evt.y, evt.x, evt.y)
if not r :
return
rid = r[0]
print(r)
myColor = can.itemcget(rid,'fill')
print(myColor)
if myColor == "yellow":
# Récupération des index ligne et colonne contenant l'id rectangle
for il, ligne in enumerate(items[0]) :
if rid in ligne :
ic = ligne.index(rid)
print(ic)
break
# Suppressions de la ligne et colonne du tableau items
# Et suppression dans le canevas
lid = items[0].pop(il)
for oid in lid :
can.delete(oid)
for ligne in items[0] :
oid = ligne.pop(ic)
can.delete(oid)
if not any(items) :
# Plus rien à faire, le tableau d'items 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
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
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 = []
colors = ['white', 'yellow']
y = DIM_LIGNE
for ligne in range(DIM_GRILLE[1]) :
x = DIM_LIGNE
ids = []
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="C/L", 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="YYYY", 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="XXXX", 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="TEXT", fill="black")
bbox = can.bbox(text_item)
i = can.create_rectangle(bbox, outline="red", fill=colors[col % 2])
can.tag_raise(text_item,i)
ids.append((i,text_item))
x += DIM_CASEL + DIM_LIGNE
items.append(ids)
y += DIM_CASEH + DIM_LIGNE
can.bind('<Button-1>', destroyer)
fenetre.mainloop() |
Partager