Esempio n. 1
0
#show image to canvas
image = Image.open(image_path)
photo = ImageTk.PhotoImage(image)  #convert to ImageTk
myimg = canvas.image([0, 0], image=photo)


#click event
def callback(event):
    print("clicked at", event.x, event.y)
    global img_file_paths

    #rotate list of image
    d = collections.deque(img_file_paths)
    d.rotate(1)
    img_file_paths = list(d)
    next_image_path = img_file_paths[-1]

    label1['text'] = next_image_path
    #update new image
    image2 = Image.open(next_image_path)
    global photo2  #very importance
    photo2 = ImageTk.PhotoImage(image2)
    canvas.itemconfigure(myimg, image=photo2)  #myimg is origin image in canvas


canvas.bind("<Button-1>", callback)  #<Button-1>: is mouse left button

g.mainloop()

#Starting with this example, write a program that takes the name of a directory and loops through all the files, displaying any files that PIL recognizes as images. You can use a try statement to catch the files PIL doesn’t recognize.
#When the user clicks on the image, the program should display the next one.