[Python] move an image between two canvases in python

Discussão em 'Python' iniciado por Stack, Fevereiro 1, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to ask how can I move one image from upper canvas to lower canvas in python? here is my code which I would like to move an image from upper canvas to lower canvas:

    from tkinter import *
    from tkinter import font
    from PIL import Image, ImageTk
    import os

    os.system("cls||clear")

    def on_drag1(event):
    cw1.moveto(image11,x = cw1.winfo_pointerx() - cw1.winfo_rootx(),
    y = cw1.winfo_pointery() - cw1.winfo_rooty())

    def on_drop1(event):
    cw2.moveto(image11,x = cw2.winfo_pointerx() - cw2.winfo_rootx(),
    y = cw2.winfo_pointery() - cw2.winfo_rooty())

    win = Tk()

    style = font.Font(size=20)

    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("E:\images\image1.jpg").
    resize((100, 100), Image.Resampling.BICUBIC))

    image11 = cw1.create_image(150, 150, image=image1,tags='image1Tag')

    cw1.bind("<B1-Motion>",on_drag1)
    cw1.bind("<ButtonRelease-1>",on_drop1)

    win.geometry("650x650")
    win.title("Draggable Widgets")
    win.mainloop()


    The problem is when I drag the image and wants to take it to lower canvas it goes behind the lower convas and will not show this image on the lower convas. Please let me know what is the problem and the remedy must take place. Thanks in advance

    Continue reading...

Compartilhe esta Página