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
| import tkinter as tk
from numpy import *
w = tk.Tk() # fenêtre principale
tableau = [0 for i in range(10)]
couleurs = {0: "white", 1: "black"}
can_width = 500 # dimensions du canevas
can_height = 500
size = 50 #taille d'une "case"
can = tk.Canvas(w, width=can_width, height=can_height) # création canevas
can.grid()
""" Fonction d'affichage du tableau """
def afficher(t):
# for j in range(tableau.shape[0]):
for i in range(len(t)):
can.create_rectangle(i * size,0,i * size + size,size,fill = couleurs[tableau[i]])
""" Fonction appelée lors d'un clic gauche sur le canevas détermine la correspondance entre la position horizontale de la souris et l'élément correspondant du tableau"""
def modifierTableau(evt):
pos_x = int(evt.x / size) #evt.x est la position en x de la souris
if tableau[pos_x] == 0: #inverser la valeur de l'élément cliqué
tableau[pos_x] = 1
else:
tableau[pos_x] = 0
afficher(tableau) #ré-afficher le tableau
afficher(tableau)
can.bind("<Button-1>", modifierTableau) # binding de la fonction modifierTableau sur le canevas
w.mainloop() # boucle principale |
Partager