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
   | import tkinter as tk
 
def colorier_centre_case(index) :
    case = cases[index]
    for i in range(4):
        if case[i]['bg'] != 'blue' :
            return
    # label
    case[4]['bg'] = 'blue'
 
 
def clic_bouton(index_case, index_element) :
    def colorier_bouton():
        bouton = cases[index_case][index_element]
        bouton.config(bg='blue', state=tk.DISABLED)
        colorier_centre_case(index_case)
    return colorier_bouton
 
 
def creer_case(index, ligne, colonne) :
    frame = tk.Frame(root)
    frame.grid(row=ligne, column=colonne)
    haut = tk.Button(frame, command=clic_bouton(index, 0), width=5)
    bas = tk.Button(frame, command=clic_bouton(index, 1), width=5)
    gauche = tk.Button(frame, command=clic_bouton(index, 2), height=5)
    droite = tk.Button(frame, command=clic_bouton(index, 3), height=5)
    centre = tk.Label(frame, width=7, height=5, bg='white')
    haut.grid(row=0, column=0, columnspan=3)
    gauche.grid(row=1, column=0)
    centre.grid(row=1, column=1)
    droite.grid(row=1, column=2)
    bas.grid(row=2, column=0, columnspan=3)
    frame.columnconfigure(2, weight=1)
    return (haut, bas, gauche, droite, centre)
 
root = tk.Tk()
 
cases = []
i = 0
for ligne in range(3):
    for colonne in range(3):
        cases.append(creer_case(i, ligne, colonne))
        i += 1
 
root.mainloop() | 
Partager