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
|
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
def change_couleur():
global c
c=(c+1)%2
c2=(c+1)%2 # pour "boucler" sur les valeurs de couleur
can1.itemconfig(f0, fill=coul[c])
can1.itemconfig(f1, fill=coul[c2])
can1.itemconfig(f2, fill=coul[c])
can1.itemconfig(f3, fill=coul[c2])
#créer les feux de départ
cc=[[50,120,60,130], [50,60,60,70], [ 140,70,150,80],[140,120,150,130]]
f0=can1.create_oval(cc[0][0],cc[0][1],cc[0][2],cc[0][3])
f1=can1.create_oval(cc[1][0],cc[1][1],cc[1][2],cc[1][3])
f2=can1.create_oval(cc[2][0],cc[2][1],cc[2][2],cc[2][3])
f3=can1.create_oval(cc[3][0],cc[3][1],cc[3][2],cc[3][3])
##i=0
##while i<len(cc):
## el = cc[i]
## can1.create_oval(el[0],el[1],el[2],el[3])
## i+=1
bou1 = Button(fen,text= 'changer',command =change_couleur)
bou1.pack(side= BOTTOM)
can1.pack()
fen.mainloop() |
Partager