Bonjour:

Me revoilà!
Je dois créer un programme de bataille navale sur python.
Je n 'en suis qu'au début...
Dans ce programme, l'utilisateur ne doit pouvoir cliquer que 18 fois sur la grille verte (pour placer ses bateaux), la fonction do_it change la couleur du bouton si il clique dessus, et donc le bouton devrait rester vert si le joueur clique une 19eme fois ...
Mais si je clique sur 25 boutons, ils deviennent tous rouge, au lieu de 18 seulement...

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
from tkinter import *
 
 
 
def set_color(button):
    def do_it():
        n=0
        if n>18:
             button['bg'] = 'green'
             n=n+1
        else:
            button['bg'] = 'red'
            n=n+1
 
    return do_it
 
 
 
def set_color2(button2):
    def do_it2():
         button2['bg'] = 'BLUE'
    return do_it2
 
 
fen= Tk()
 
 
F3=Frame(fen)
F4=Frame(fen)
F1=Frame(fen)
F2=Frame(fen)
 
color = 'green'
color2='white'
 
 
text1=Label(F3,text="Voici votre grille,veuillez placer vos bateau")
text1.grid(row=1,column=1)
 
var1 = IntVar()
B1 = Checkbutton(F3, text = "Porte-avions(5 cases)", variable = var1)
B1.grid(row=2, sticky=W)
var2 = IntVar()
B2 = Checkbutton(F3, text = "croiseur(4cases)", variable = var2)
B2.grid(row=3, sticky=W)
var3 = IntVar()
B3 = Checkbutton(F3, text = "contre-torpilleurs(3 cases)", variable = var3)
B3.grid(row=4, sticky=W)
var4 = IntVar()
B4 = Checkbutton(F3, text = "sous-marin(3 cases)", variable = var4)
B4.grid(row=5, sticky=W)
var5 = IntVar()
B5 = Checkbutton(F3, text = "torpilleur(2 cases)", variable = var5)
B5.grid(row=6, sticky=W)
 
text1=Label(F4,text="Voici la grille de votre adversaire.")
text1.grid(row=1,column=1)
text2=Label(F4,text="Quand ce serra a votre tour, vous pourrez cliquer sur la case de votre choix pour attaquer")
text2.grid(row=2,column=1)
text3=Label(F4,text="La case deviendra bleu si vous n'avez touché aucun bateau de l'adversiare.")
text3.grid(row=3,column=1)
text4=Label(F4,text="Elle deviedra noire si vous avez touché un de ses bateau.")
text4.grid(row=4,column=1)
 
for x in range(11):
    for y in range(11):
        button = Button(F1,bd=5,width=7,height=3, bg=color)
        button['command']=set_color(button) 
        button.grid(row=y, column=x)
for x in range(13,14):
    for y in range (11):
        button3 = Button(F1,bd=0,width=7,height=3)
        button3.grid(row=y,column=x)
 
 
for x in range(11):
    for y in range(11):
        button2 = Button(F2,bd=5,width=7,height=3, bg=color2)
        button2['command']=set_color2(button2) 
        button2.grid(row=y, column=x)
 
 
 
F3.grid(row=1,column=1)
F4.grid(row=1,column=2)
F2.grid(row=2,column=2)
F1.grid(row=2,column=1)
 
fen.mainloop()
C'est donc sur la fonction do_it que je bloque...
Merci pour votre aide!

Bonne journée!