Bonjour je suis en Terminale S et je me suis lancé un défi qui est de créer un démineur avec Python et Tkinter, J'ai déja assez avancé le projet et je me retrouve a un endroit ou le code ne marche plus et je ne comprends pas pourquoi
A partir du moment ou je modifie l'image du bouton sur lequel j'ai cliqué, le programme marche encore mais quand je clique sur un deuxième bouton, l'image disparait du premier ou j'ai cliqué et se retrouve sur le deuxième. Je n'arrive pas a afficher une image sur 2 boutons a la fois en même temps
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 def bombTest(x,y): global bombs,gameC,but,temporaryP,temporaryB caseBCount = 0 temporaryB = Button(gameC,width=20,height=20,image=but,bd=0,relief=FLAT,overrelief=SUNKEN,state="disabled") temporaryB.grid(row=x,column=y) xt,yt = temporaryB.grid_info()["column"],temporaryB.grid_info()["row"] print([xt,yt]) if [xt,yt] not in bombs: temporaryP = F.COUNT_ADJACENT_BOMBS(xt,yt,bombs) temporaryB = Button(gameC,width=20,height=20,command=TEST,image=temporaryP,bd=0,relief=FLAT,overrelief=SUNKEN) #MODIFIER L'IMAGE DU BOUTON temporaryB.grid(row=x,column=y) #REMETTRE LE BOUTON A SA PLACE else: print("non")
Cela ne génère aucune erreur de compilation ou même d'erreur dans la console lors de l'exécution mais quelque chose doit m'échapper...
Voila le code complet au cas ou l'erreur vienne de la
Ainsi que la bibliothèque F_TE que j'ai crée pour rendre le code plus propre
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 from tkinter import * import random import F_TE as F Win = Tk() flag = 0 def play(): flag = 1; randBombing() for j in range(10): for i in range(10): temporaryB = Button(gameC,width=20,height=20,command=lambda x=j,y=i:bombTest(x,y),image=but,bd=0,relief=FLAT,overrelief=SUNKEN) temporaryB.grid(row=j,column=i) def randBombing(): bombCount = 15 global bombs for r in range(bombCount): xb,yb = random.randint(0,9),random.randint(0,9) if [xb,yb] not in bombs: bombs += [[xb,yb]] else: xb,yb = random.randint(0,9),random.randint(0,9) if [xb,yb] not in bombs: bombs += [[xb,yb]] print(bombs) def bombTest(x,y): global bombs,gameC,but,temporaryP,temporaryB caseBCount = 0 temporaryB = Button(gameC,width=20,height=20,image=but,bd=0,relief=FLAT,overrelief=SUNKEN,state="disabled") temporaryB.grid(row=x,column=y) xt,yt = temporaryB.grid_info()["column"],temporaryB.grid_info()["row"] print([xt,yt]) if [xt,yt] not in bombs: temporaryP = F.COUNT_ADJACENT_BOMBS(xt,yt,bombs) temporaryB = Button(gameC,width=20,height=20,command=TEST,image=temporaryP,bd=0,relief=FLAT,overrelief=SUNKEN) temporaryB.grid(row=x,column=y) else: print("non") ################################################################################ gameF = Frame(Win,width=100,height=100) gameF.grid() gameOF = LabelFrame(Win,width=100,height=220,text="OPTIONS") gameOF.grid(row=0,column=1) gameC = Canvas(gameF,width=100,height=100) gameC.grid() but = PhotoImage(file="BUT.png") for j in range(10): for i in range(10): Button(gameC,width=20,height=20,command=Win.destroy,image=but,bd=0,relief=FLAT,overrelief=SUNKEN,state="disabled").grid(row=j,column=i) playB = Button(gameOF,width=25,height=4,text="PLAY",command=play) playB.grid() replayB = Button(gameOF,width=25,height=4,text="REPLAY",command=Win.destroy) replayB.grid(row=1) quitB = Button(gameOF,width=25,height=4,text="QUIT",command=Win.destroy) quitB.grid(row=2) bombs = [] temporaryP = PhotoImage() temporaryB = Button() ################################################################################ Win.mainloop()
Merci d'avance si vous vous penchez sur mon cas
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 from tkinter import * def COUNT_ADJACENT_BOMBS(x,y,bombs): c = 0 ifinal = PhotoImage() i1 = PhotoImage(file="1.png") ## i2 = PhotoImage(file="2.png") ## i3 = PhotoImage(file="3.png") ## i4 = PhotoImage(file="4.png") ## i5 = PhotoImage(file="5.png") ## i6 = PhotoImage(file="6.png") ## i7 = PhotoImage(file="7.png") ## i8 = PhotoImage(file="8.png") ################################COMPTAGE if [x-1,y-1] in bombs: c += 1 if [x-1,y] in bombs: c += 1 if [x-1,y+1] in bombs: c += 1 if [x,y-1] in bombs: c += 1 if [x,y+1] in bombs: c += 1 if [x+1,y-1] in bombs: c += 1 if [x+1,y] in bombs: c += 1 if [x+1,y+1] in bombs: c += 1 ################################COMPTAGE ################################DEF IMAGE if c == 1: #POUR L'INSTANT ATTRIBUE LA MÊME IMAGE DANS CHAQUE CAS ifinal = i1 if c == 2: ifinal = i1 if c == 3: ifinal = i1 if c == 4: ifinal = i1 if c == 5: ifinal = i1 if c == 6: ifinal = i1 if c == 7: ifinal = i1 if c == 8: ifinal = i1 ################################DEF IMAGE return ifinal
EDIT: J'ai résolu le problème, en effet les instances PhotoImage se supprimaient a chaque fois, une ligne et c'est corrigé
Partager