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
| from tkinter import *
def feux(x,y,d) :
"fonction qui dessine les feux"
can.coords(rond,x,y,x+d,y+d)
def changer() :
"fonction qui change les feux de couleurs"
global x,y,d,a
a = 0
a = not a
if a == 0:
feux(75,425,25)
feux(900,250,25)
feux(50,190,50)
can.itemconfigure(rond,fill='red')
feux(900,460,50)
can.itemconfigure(rond,fill='red')
if a == 1 :
feux(75,425,25)
can.itemconfigure(rond,fill='red')
feux(900,250,25)
can.itemconfigure(rond,fill='red')
feux(50,190,50)
feux(900,460,50)
x,y,d = 0,0,0
fen = Tk()
can = Canvas(fen,bg='gray75',width=1000,height=700)
can.pack()
# dessin de la route
route = can.create_rectangle(100,50,900,650,fill='gray40')
# dessin du rond symbolisant le feux de circulation
rond = can.create_oval(x,y,x+d,y+d,fill='green')
# dessin du passage piéton :
x1,x2 = 120,180
i = 0
while i < 800 :
can.create_rectangle(x1+i,250,x2+i,450,fill='light yellow')
i = i + 100
# création du bouton pour changer les couleurs de feux
bou = Button(fen,text='Changer',borderwidth=5,command=changer)
bou.pack(side=BOTTOM)
fen.mainloop() |