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
|
from Tkinter import *
from PIL import Image, ImageTk
def poubelle_1():
can1.create_rectangle(10,250,300,600,fill='yellow')
can1.create_rectangle(20,280,290,500,fill='black')
def poubelle_2():
can1.create_rectangle(400,250,700,600,fill='green')
can1.create_rectangle(410,280,690,500,fill='black')
def poubelle_3():
can1.create_rectangle(790,250,1090,600,fill='grey')
can1.create_rectangle(800,280,1080,500,fill='black')
def clic(event):
global DETECTION_CLIC_SUR_OBJET
X = event.x
Y = event.y
[xmin,ymin,xmax,ymax] = can1.coords(IMAGE_1)
[xmin1,ymin1,xmax1,ymax1] = can1.coords(IMAGE_2)
[xmin2,ymin2,xmax2,ymax2] = can1.coords(IMAGE_3)
[xmin3,ymin3,xmax3,ymax3] = can1.coords(IMAGE_4)
[xmin4,ymin4,xmax4,ymax4] = can1.coords(IMAGE_5)
[xmin5,ymin5,xmax5,ymax5] = can1.coords(IMAGE_6)
if xmin<=X<=xmax and ymin<=Y<=ymax or xmin1<=X<=xmax1 and ymin1<=Y<=ymax1 or xmin2<=X<=xmax2 and ymin2<=Y<=ymax2 or xmin3<=X<=xmax3 and ymin3<=Y<=ymmax3 or xmin4<=X<=xmax4 and ymin4<=Y<=ymax4 or xmin5<=X<=xmax5 and ymin5<=Y<=ymax5 : DETECTION_CLIC_SUR_OBJET = True
else : DETECTION_CLIC_SUR_OBJET = False
def drag(event):
X = event.x
Y = event.y
if DETECTION_CLIC_SUR_OBJET == True :
if X<0: X=0
if X>1100: X=1100
if Y<0: Y=0
if Y>600: Y=600
can1.coords(IMAGE_1,X-128,Y-190,X+128,Y+190)
can1.coords(IMAGE_2,X-123,Y-184,X+123,Y+184)
can1.coords(IMAGE_3,X-239,Y-194,X+239,Y+194)
can1.coords(IMAGE_4,X-200,Y-200,X+200,Y+200)
can1.coords(IMAGE_5,X-108,Y-180,X+108,Y+180)
can1.coords(IMAGE_6,X-188,Y-188,X+188,Y+188)
DETECTION_CLIC_SUR_OBJET = False
fen1 = Tk()
image = Image.open("banane_petite.jpg")
image1 = Image.open("bouteille_verre.jpg")
image2 = Image.open("carton.jpg")
image3 = Image.open("bouteille_plastique.jpg")
image4 = Image.open("canette.jpg")
image5 = Image.open("journal.jpg")
photo = ImageTk.PhotoImage(image)
photo1 = ImageTk.PhotoImage(image1)
photo2 = ImageTk.PhotoImage(image2)
photo3 = ImageTk.PhotoImage(image3)
photo4 = ImageTk.PhotoImage(image4)
photo5 = ImageTk.PhotoImage(image5)
can1 = Canvas(fen1,bg='white',height=600,width=1100)
IMAGE_1 = can1.create_image(60,100,image=photo)
IMAGE_2 = can1.create_image(200,100,image=photo1)
IMAGE_3 = can1.create_image(360,100,image=photo2)
IMAGE_4 = can1.create_image(560,100,image=photo3)
IMAGE_5 = can1.create_image(750,100,image=photo4)
IMAGE_6 = can1.create_image(950,100,image=photo5)
can1.bind('<Button-1>',clic)
can1.bind('<B1-Motion>',drag)
can1.focus_set()
can1.pack(side=TOP)
poubelle_1()
poubelle_2()
poubelle_3()
bou1 = Button(fen1,text='Quitter',command=fen1.quit)
bou1.pack(side=BOTTOM)
fen1.mainloop()
fen1.destroy() |
Partager