Hi, Im creating a canvas and inserting an image. I'm using bind to move the image with my mouse. It works, but whenever I add another image, it doesn't work. When I try to move any images, only image1 moves and image 2 dissappears.

Any ideas would help, thanks

Here's my code:

from tkinter import *

root = Tk()
root.title("Testing...")
root.geometry("800x600")
w = 600
h = 400
x = w / 2
y = h / 2

my_canvas = Canvas(root, width=w, height=h, bg="white")
my_canvas.pack(pady=20)

img = PhotoImage(file="bbishop.png")
my_image = my_canvas.create_image(260, 125, anchor=NW, image=img)
img2 = PhotoImage(file="brook.png")
my_image2 = my_canvas.create_image(200, 325, anchor=NW, image=img2)

def move(e):
    # e.x
    # e.y
    global img, img2
    img = PhotoImage(file="bbishop.png")
    my_image = my_canvas.create_image(e.x, e.y, image=img)
    #my_label.config(text="Coordinates: x: " + str(e.x) + " y: " + str(e.y))
    img2 = PhotoImage(file="brook.png")
    my_image2 = my_canvas.create_image(e.x, e.y, 325, image=img2)

my_label = Label(root, text="")
my_label.pack(pady=20)

my_canvas.bind('<B1-Motion>', move)
root.mainloop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.