问题是当我拖动图像并希望将其拖到较低的帆布时,它会落后于下部的轴,并且不会在下部的轴上显示此图像。 请让我知道问题是什么,必须发生补救措施。 预先感谢
from tkinter import *
from PIL import Image, ImageTk
def on_drag(event):
""" Moves the image inside cw1 """
x, y = event.x, event.y
cw1.coords(image_id, x, y)
def on_drop(event):
global image_id
x, y = event.x, event.y
abs_x = cw1.winfo_rootx() + x
abs_y = cw1.winfo_rooty() + y
cw2_x = cw2.winfo_rootx()
cw2_y = cw2.winfo_rooty()
cw2_width = cw2.winfo_width()
cw2_height = cw2.winfo_height()
if cw2_x <= abs_x <= cw2_x + cw2_width and cw2_y <= abs_y <= cw2_y + cw2_height:
cw1.delete(image_id)
new_x = abs_x - cw2_x
new_y = abs_y - cw2_y
image_id = cw2.create_image(new_x, new_y, image=image1, tags='image1Tag')
win = Tk()
win.geometry("650x650")
win.title("Draggable Image Between Canvases")
cw1 = Canvas(win, width=350, height=350, bg="khaki2")
cw1.pack(fill="both", expand=True)
cw2 = Canvas(win, width=350, height=350, bg="salmon")
cw2.pack(fill="both", expand=True)
image1 = ImageTk.PhotoImage(Image.open("plot.png").resize((100, 100), Image.Resampling.BICUBIC))
image_id = cw1.create_image(150, 150, image=image1, tags='image1Tag')
cw1.tag_bind(image_id, "<B1-Motion>", on_drag)
cw1.tag_bind(image_id, "<ButtonRelease-1>", on_drop)
win.mainloop()