Bonjour, toujours dans le livre de Swinnen
Créer un passage piéton avec feux alternatif et bouton pour changer a couleur
Voila mon code qui fonctionne ....mais kilometrique
Des pistes pour le rendre plus compact...?
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
 
from tkinter import *
 
coul =['red','green']
c=0
 
fen = Tk()
can1= Canvas(fen, width=200, height=200, bg='light grey')#le canevas
 
can1.create_rectangle(50, 0, 150, 200, fill="grey" )# la route
#le passage pieton
i,x =0,52.5
while i<7:
    can1.create_rectangle(x,80,x+10,120, fill = 'white')
    i+=1
    x+=14
 
 
 
def changer_couleur():
    global c
 
    if c== 0:
        x1,y1  = 50,120
        feu1=can1.create_oval(x1,y1,x1+10,y1+10 , width = 3,fill = coul[1])
        x3,y3  = 50,60
        feu2=can1.create_oval(x3,y3,x3+20,y3+20 , width = 3,fill =coul[0])
        x2,y2  = 140,70
        feu1=can1.create_oval(x2,y2,x2+10,y2+10 , width = 3,fill = coul[1])
        feu2=x4,y4  = 140,120
        can1.create_oval(x4,y4,x4+20,y4+20 , width = 3,fill = coul[0])
        c=1
    else:
        x1,y1  = 50,120
        feu1=can1.create_oval(x1,y1,x1+10,y1+10 , width = 3,fill = coul[0])
        x3,y3  = 50,60
        feu2=can1.create_oval(x3,y3,x3+20,y3+20 , width = 3,fill =coul[1])
        x2,y2  = 140,70
        feu1=can1.create_oval(x2,y2,x2+10,y2+10 , width = 3,fill = coul[0])
        feu2=x4,y4  = 140,120
        can1.create_oval(x4,y4,x4+20,y4+20 , width = 3,fill = coul[1])
        c=0
 
#créer les feux de départ        
x1,y1  = 50,120
feu1=can1.create_oval(x1,y1,x1+10,y1+10 , width = 3,fill = coul[0])
x3,y3  = 50,60
feu2=can1.create_oval(x3,y3,x3+20,y3+20 , width = 3,fill =coul[1])
x2,y2  = 140,70
feu1=can1.create_oval(x2,y2,x2+10,y2+10 , width = 3,fill = coul[0])
feu2=x4,y4  = 140,120
can1.create_oval(x4,y4,x4+20,y4+20 , width = 3,fill = coul[1])
 
bou1 = Button(fen,text= 'changer',command =changer_couleur)
bou1.pack(side= BOTTOM)
 
can1.pack()    
 
fen.mainloop()
En tenant compte que je débute en python et que je n'ai que peu "d'outils" à ma disposition.