需要解决在两个画布间移动图片的问题,已经让AI帮我用各种方法解决,但还是没有实现,特请各位专家帮助

#需要实现真正的移动,而不是在第二区重新加载,在第一区删除
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk

# 全局变量用于存储图片的位置
x1, y1 = 0, 0
x2, y2 = 0, 0
# 用于标记图片当前所在的画布
current_canvas = None

# 拖动图片的函数
def drag_start(event, canvas, img_id, x_var, y_var):
    global x1, y1, x2, y2
    if canvas == canvas1:
        x1 = event.x
        y1 = event.y
    else:
        x2 = event.x
        y2 = event.y

def drag_motion(event, canvas, img_id, x_var, y_var):
    global x1, y1, x2, y2
    dx = event.x - (x1 if canvas == canvas1 else x2)
    dy = event.y - (y1 if canvas == canvas1 else y2)
    canvas.move(img_id, dx, dy)
    if canvas == canvas1:
        x1 = event.x
        y1 = event.y
        x_var.set(x1)
        y_var.set(y1)
    else:
        x2 = event.x
        y2 = event.y
        x_var.set(x2)
        y_var.set(y2)

# 移动图片的函数
def move_image():
    global current_canvas
    if current_canvas == canvas1:
        # 获取当前图片在第一个画布的坐标
        x, y = canvas1.coords(img_id1)
        # 计算图片在第二个画布中的相对坐标(这里假设左上角为目标位置)
        target_x = 0
        target_y = 0
        # 将图片从第一个画布提升到第二个画布
        canvas2.lift(img_id1)
        # 重新设置图片在第二个画布中的坐标
        canvas2.coords(img_id1, target_x, target_y)
        current_canvas = canvas2
        x1_var.set(target_x)
        y1_var.set(target_y)
    elif current_canvas == canvas2:
        # 获取当前图片在第二个画布的坐标
        x, y = canvas2.coords(img_id1)
        # 计算图片在第一个画布中的相对坐标(这里假设左上角为目标位置)
        target_x = 0
        target_y = 0
        # 将图片从第二个画布提升到第一个画布
        canvas1.lift(img_id1)
        # 重新设置图片在第一个画布中的坐标
        canvas1.coords(img_id1, target_x, target_y)
        current_canvas = canvas1
        x1_var.set(target_x)
        y1_var.set(target_y)
    elif current_canvas is None:
        # 初始情况,图片默认在第一个画布
        current_canvas = canvas1


# 创建主窗口
root = tk.Tk()
root.title("图片移动示例")

# 创建两个 Frame 框架
frame1 = tk.Frame(root, width=200, height=200, bd=2, relief="groove")
frame1.pack(side=tk.LEFT, padx=10, pady=10)

frame2 = tk.Frame(root, width=200, height=200, bd=2, relief="groove")
frame2.pack(side=tk.LEFT, padx=10, pady=10)

# 创建 Canvas 用于显示图片
canvas1 = tk.Canvas(frame1, width=200, height=200)
canvas1.pack()

canvas2 = tk.Canvas(frame2, width=200, height=200)
canvas2.pack()

# 加载图片
try:
    image1 = Image.open("example1.png")
    photo1 = ImageTk.PhotoImage(image1)

    image2 = Image.open("example2.png")
    photo2 = ImageTk.PhotoImage(image2)
except FileNotFoundError:
    print("图片文件未找到,请确保 example1.png 和 example2.png 存在。")
    root.destroy()
    exit()

# 在 Canvas 上显示图片
img_id1 = canvas1.create_image(0, 0, anchor=tk.NW, image=photo1)
img_id2 = canvas2.create_image(0, 0, anchor=tk.NW, image=photo2)
current_canvas = canvas1

# 绑定拖动事件
canvas1.tag_bind(img_id1, "<ButtonPress-1>", lambda event: drag_start(event, canvas1, img_id1, x1_var, y1_var))
canvas1.tag_bind(img_id1, "<B1-Motion>", lambda event: drag_motion(event, canvas1, img_id1, x1_var, y1_var))

canvas2.tag_bind(img_id2, "<ButtonPress-1>", lambda event: drag_start(event, canvas2, img_id2, x2_var, y2_var))
canvas2.tag_bind(img_id2, "<B1-Motion>", lambda event: drag_motion(event, canvas2, img_id2, x2_var, y2_var))

# 创建坐标跟踪标签
x1_var = tk.IntVar()
y1_var = tk.IntVar()
x2_var = tk.IntVar()
y2_var = tk.IntVar()

label1 = ttk.Label(root, textvariable=x1_var)
label1.pack()
label2 = ttk.Label(root, textvariable=y1_var)
label2.pack()
label3 = ttk.Label(root, textvariable=x2_var)
label3.pack()
label4 = ttk.Label(root, textvariable=y2_var)
label4.pack()

# 创建移动按钮
button = ttk.Button(root, text="移动", command=move_image)
button.pack(pady=10)

# 运行主循环
root.mainloop()
讨论数量: 1
Jason990420

In tkinter, a figure (such as a shape or drawing) cannot directly move between different canvases because each canvas operates independent.

For example

  • The figure in expression canvas1.coords(img_id1) is different from the figure in canvas2.coords(img_id1).
2周前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!