Salut à tous, j'ai un problème assez énervant,
J'ai créer une ihm assez compliqué (pour mon niveau) et je souhaiterais faire tourner un petit canvas qui appelle une fonction nommée Clavier. cette fonction, elle se trouve dans le module Inter. Le problème se situe au niveau du Canevas.bind du premier code (l.46).
Rien ne vaut un bon vieux code donc voici mes deux modules :
Module 1 que je lance. (IHM)
Module 2 (Inter) :
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 from Tkinter import * from Inter import * # position initiale des champions PosX = 10 PosY = 10 PosX1 = 990 PosY1 = 610 #Variables g?n?rales actuTour=0 #******************************************************************************# #* *# #* * * * PROGRAMME PRINCIPAL * * * *# #* *# #******************************************************************************# # Creation de la fenetre windows Jeu=Tk() Jeu.title("CheckMat") Jeu.iconbitmap("CheckMate.ico") Jeu.geometry("1200x720") Jeu.resizable(width=False,height=False) Jeu.configure() # Creation des frames principales frame3 = Frame(Jeu, bg="yellow", width=200, height=720) frame3.pack(side=LEFT) frame1 = Frame(Jeu, bg="black", width=1000, height=620) frame1.pack(side=BOTTOM) Canevas = Canvas(frame1, width = 1000, height =620, bg ='white') photo = PhotoImage(file="TRY.gif") Canevas.create_image(0, 0, anchor=NW, image=photo) Pion = Canevas.create_oval(PosX-10,PosY-10,PosX+10,PosY+10,width=2,outline='black',fill='red') Pionne = Canevas.create_oval(PosX1-10,PosY1-10,PosX1+10,PosY1+10,width=2,outline='black',fill='green') for i in range(0,1000,20): Canevas.create_line(i,0,i,620,fill="#000000") for j in range(0,620,20): Canevas.create_line(0,j,1000,j,fill="#000000") Canevas.focus_set() Canevas.bind('<Key>',Clavier) Canevas.pack() Jeu.mainloop()
Comment réussir à faire exécuter l'évent Clavier par l'action sur le canevas ? Merci d'avance, je souhaite réellement faire ceci en deux modules séparés. (projet d'isn de fin d'année : Jeu vidéo ).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 from IHM import actuTour import time as t def Clavier(event): ... def decompte(count=3): ... def Initialisation(): ...
En complément, mes codes sources si vous souhaitez tester le tout.
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
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 from Tkinter import * from Inter import * #******************************************************************************# #* *# #* * * * VARIABLES INITIALES * * * *# #* *# #******************************************************************************# # position initiale des champions PosX = 10 PosY = 10 PosX1 = 990 PosY1 = 610 #Variables g?n?rales actuTour=0 #******************************************************************************# #* *# #* * * * PROGRAMME PRINCIPAL * * * *# #* *# #******************************************************************************# # Creation de la fenetre windows Jeu=Tk() Jeu.title("CheckMat") Jeu.iconbitmap("CheckMate.ico") Jeu.geometry("1200x720") Jeu.resizable(width=False,height=False) Jeu.configure() # Creation des frames principales frame3 = Frame(Jeu, bg="yellow", width=200, height=720) frame3.pack(side=LEFT) frame1 = Frame(Jeu, bg="black", width=1000, height=620) frame1.pack(side=BOTTOM) Canevas = Canvas(frame1, width = 1000, height =620, bg ='white') photo = PhotoImage(file="TRY.gif") Canevas.create_image(0, 0, anchor=NW, image=photo) Pion = Canevas.create_oval(PosX-10,PosY-10,PosX+10,PosY+10,width=2,outline='black',fill='red') Pionne = Canevas.create_oval(PosX1-10,PosY1-10,PosX1+10,PosY1+10,width=2,outline='black',fill='green') for i in range(0,1000,20): Canevas.create_line(i,0,i,620,fill="#000000") for j in range(0,620,20): Canevas.create_line(0,j,1000,j,fill="#000000") Canevas.focus_set() Canevas.bind('<Key>',Clavier) Canevas.pack() frame2 = Frame(Jeu, bg="blue", width=1000, height=100) frame2.pack() # Creation des frames secondaires # Frames de gauches framea = Frame(frame3, bg="yellow",width=200, height=70) framea.pack (side=TOP) photo1 = PhotoImage(file="test1.gif") Canfra = Canvas(framea, width=200, height=70, bg="yellow") item1 = Canfra.create_image(0,0,anchor=NW, image=photo1) print(item1) Canfra.pack() frameb = Frame(frame3, bg="green",width=200, height=90) frameb.pack (side=TOP) framec = Frame(frame3, bg="red",width=200, height=90) framec.pack (side=TOP) framed = Frame(frame3, bg="green",width=200, height=340) framed.pack (side=TOP) framee = Frame(frame3, bg="white",width=200, height=130) framee.pack () # Frames du haut # Frames de la longueur framef = Frame(frame2, bg="red",width=90, height=100) framef.pack (side=LEFT) frameg = Frame(frame2, bg="green",width=90, height=100) frameg.pack (side=LEFT) frameh = Frame(frame2, bg="white",width=450, height=100) frameh.pack (side=LEFT) framei = Frame(frame2, bg="green",width=380, height=100) framei.pack () # Frames des sorts/tour framej = Frame(frameh, bg="black",width=450, height=65) framej.pack (side=TOP) framek = Frame(frameh, bg="white",width=450, height=35) framek.pack (expand=1) framel = Frame(framej, bg="red",width=90, height=65) framel.pack (side=LEFT) framem = Frame(framej, bg="black",width=90, height=65) framem.pack (side=LEFT) framen = Frame(framej, bg="red",width=90, height=65) framen.pack (side=LEFT) frameo = Frame(framej, bg="black",width=90, height=65) frameo.pack (side=LEFT) framep = Frame(framej, bg="red",width=90, height=65) framep.pack () Jeu.mainloop()
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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238 from IHM import actuTour import time as t def Clavier(event): """ Gestion de l'?v?nement Appui sur une touche du clavier """ global PosX,PosY,PosX1,PosY1,PosX2,PosY2,PosX3,PosY3,move touche = event.keysym print(touche) if (actuTour == 0) : if move > 0 : # d?placement vers le haut if touche == 'Up': PosY -= 20 move=move-1 if (PosX==PosX1 and PosY==PosY1) or (PosX==PosX2 and PosY==PosY2) or (PosX==PosX3 and PosY==PosY3) : PosY += 20 move=move+1 lab1.config(text="pas possible") else : lab1.config(text="Sa roule") # d?placement vers le bas if touche == 'Down': PosY += 20 move=move-1 if (PosX==PosX1 and PosY==PosY1) or (PosX==PosX2 and PosY==PosY2) or (PosX==PosX3 and PosY==PosY3) : PosY -= 20 move=move+1 lab1.config(text="pas possible") else : lab1.config(text="Sa roule") # d?placement vers la droite if touche == 'Right': PosX += 20 move=move-1 if (PosX==PosX1 and PosY==PosY1) or (PosX==PosX2 and PosY==PosY2) or (PosX==PosX3 and PosY==PosY3) : PosX -= 20 move=move+1 lab1.config(text="pas possible") else : lab1.config(text="Sa roule") # d?placement vers la gauche if touche == 'Left': PosX -= 20 move=move-1 if (PosX==PosX1 and PosY==PosY1) or (PosX==PosX2 and PosY==PosY2) or (PosX==PosX3 and PosY==PosY3) : PosX += 20 move=move+1 lab1.config(text="pas possible") else : lab1.config(text="Sa roule") # on dessine le pion ? sa nouvelle position Canevas.coords(Pion, PosX -10, PosY -10, PosX +10, PosY +10) lab2.config(text=move) else : # d?placement vers le haut if touche == 'Up' or touche == 'Down' or touche == 'Right' or touche == 'Left' : lab2.config(text='Impossible') elif actuTour == 1 : if move >0 : # d?placement vers le haut if touche == 'Up': PosY1 -= 20 move=move-1 if (PosX1==PosX and PosY1==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX1==PosX3 and PosY1==PosY3) : PosY1 += 20 move=move+1 lab1.config(text="pas possible") else : lab1.config(text="Sa roule") # d?placement vers le bas if touche == 'Down': PosY1 += 20 move=move-1 if (PosX1==PosX and PosY1==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX1==PosX3 and PosY1==PosY3) : PosY1 -= 20 move=move+1 lab1.config(text="pas possible") else : lab1.config(text="Sa roule") # d?placement vers la droite if touche == 'Right': PosX1 += 20 move=move-1 if (PosX1==PosX and PosY1==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX1==PosX3 and PosY1==PosY3) : PosX1 -= 20 move=move+1 lab1.config(text="pas possible") else : lab1.config(text="Sa roule") # d?placement vers la gauche if touche == 'Left': PosX1 -= 20 move=move-1 if (PosX1==PosX and PosY1==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX1==PosX3 and PosY1==PosY3) : PosX1 += 20 move=move+1 lab1.config(text="pas possible") else : lab1.config(text="Sa roule") # on dessine le pion ? sa nouvelle position Canevas.coords(Pionne, PosX1 -10, PosY1 -10, PosX1 +10, PosY1 +10) lab2.config(text=move) else : # d?placement vers le haut if touche == 'Up' or touche == 'Down' or touche == 'Right' or touche == 'Left' : lab2.config(text='Impossible') elif actuTour == 2 : if move > 0 : # d?placement vers le haut if touche == 'Up': PosY2 -= 20 move=move-1 if (PosX2==PosX and PosY2==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX2==PosX3 and PosY2==PosY3) : PosY2 += 20 move=move+1 lab1.config(text="pas possible") else : lab1.config(text="Sa roule") # d?placement vers le bas if touche == 'Down': PosY2 += 20 move=move-1 if (PosX2==PosX and PosY2==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX2==PosX3 and PosY2==PosY3) : PosY2 -= 20 move=move+1 lab1.config(text="pas possible") else : lab1.config(text="Sa roule") # d?placement vers la droite if touche == 'Right': PosX2 += 20 move=move-1 if (PosX2==PosX and PosY2==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX2==PosX3 and PosY2==PosY3) : PosX2 -= 20 move=move+1 lab1.config(text="pas possible") else : lab1.config(text="Sa roule") # d?placement vers la gauche if touche == 'Left': PosX2 -= 20 move=move-1 if (PosX2==PosX and PosY2==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX2==PosX3 and PosY2==PosY3) : PosX2 += 20 move=move+1 lab1.config(text="pas possible") else : lab1.config(text="Sa roule") # on dessine le pion ? sa nouvelle position Canevas.coords(Pion1, PosX2 -10, PosY2 -10, PosX2 +10, PosY2 +10) lab2.config(text=move) else : # d?placement vers le haut if touche == 'Up' or touche == 'Down' or touche == 'Right' or touche == 'Left' : lab2.config(text='Impossible') elif actuTour == 3 : if move > 0 : # d?placement vers le haut if touche == 'Up': PosY3 -= 20 move=move-1 if (PosX3==PosX and PosY3==PosY) or (PosX3==PosX2 and PosY3==PosY2) or (PosX1==PosX3 and PosY1==PosY3) : PosY3 += 20 move=move+1 lab1.config(text="pas possible") else : lab1.config(text="Sa roule") # d?placement vers le bas if touche == 'Down': PosY3 += 20 move=move-1 if (PosX3==PosX and PosY3==PosY) or (PosX3==PosX2 and PosY3==PosY2) or (PosX1==PosX3 and PosY1==PosY3) : PosY3 -= 20 move=move+1 lab1.config(text="pas possible") else : lab1.config(text="Sa roule") # d?placement vers la droite if touche == 'Right': PosX3 += 20 move=move-1 if (PosX3==PosX and PosY3==PosY) or (PosX3==PosX2 and PosY3==PosY2) or (PosX1==PosX3 and PosY1==PosY3) : PosX3 -= 20 move=move+1 lab1.config(text="pas possible") else : lab1.config(text="Sa roule") # d?placement vers la gauche if touche == 'Left': PosX3 -= 20 move=move-1 if (PosX3==PosX and PosY3==PosY) or (PosX3==PosX2 and PosY3==PosY2) or (PosX1==PosX3 and PosY1==PosY3) : PosX3 += 20 move=move+1 lab1.config(text="pas possible") else : lab1.config(text="Sa roule") # on dessine le pion ? sa nouvelle position Canevas.coords(Pion2, PosX3 -10, PosY3 -10, PosX3 +10, PosY3 +10) lab2.config(text=move) else : # d?placement vers le haut if touche == 'Up' or touche == 'Down' or touche == 'Right' or touche == 'Left' : lab2.config(text='Impossible') def decompte(count=3): global actuTour lab.config(text=str(count)) if count >= 0 : Mafenetre.after(1000,decompte, count-1) else : actuTour=actuTour+1 Initialisation() if actuTour >= 4 : actuTour=0 decompte() def Initialisation(): global move,Dep1,Dep2,Dep3,Dep4, x if actuTour==0 : move = Dep1 elif actuTour==1 : move = Dep2 elif actuTour==2 : move = Dep3 elif actuTour==3 : move = Dep4 lab.config(text=move) move=3 #déplacement des joueurs Dep1 = 3 Dep2 = 7 Dep3 = 2 Dep4 = 4
Merci d'avance![]()
Partager