使用鼠标在框架中移动缩放图像

Use mouse to move a zoomed image in a frame

我有一个比例尺可以控制 Tkinter 框架中图像的放大和缩小。我想用鼠标单击图像并在框架中移动缩放图像,但它不起作用。下面是我正在使用的代码。任何让它工作的帮助将不胜感激。提前致谢。下面是我当前的代码。

root = Tk()
canvas = tk.Canvas(root, height=500, width=500)
canvas.pack()
frame = customtkinter.CTkFrame(master=root, width=300, height=200)
frame.place(x=10, y=10)

canvas.bind('<ButtonPress-1>', move_from)
canvas.bind('<B1-Motion>', move_to)

###putting image on frame label###
def Image():
    global img, img_label, IMG
    IMG = Image.open('im.png')
    img = ImageTk.PhotoImage(IMG.resize((280, 280), Image.ANTIALIAS))
    img_label = Label(frame, image=img)
    img_label.image = img
    img_label.place(x=200, y=0)
    img_label.pack(side=TOP, padx=20, pady=50)
    return img_label

#### zoom function ####
def zoom_img(zoom):
    global Img
    newsize = (IMG.size[0]* int(zoom), 
                IMG.size[1]*int(zoom))
    scaledIMG = IMG.resize(newsize, Image.LINEAR)
    Img = ImageTk.PhotoImage(scaledIMG)
    img_label.configure(image=Img, width=200, height=220)
    img_label.place(x=80, y=0)
    img_label.pack(side=TOP, padx=5, pady=5)

### scale ###
scale_label = customtkinter.CTkLabel(master=root, text="Zoom", width=50, fg_color="gray80").place(x=85, y=130)

var = StringVar()
scale = tk.Scale(root, variable=var, orient='horizontal', from_=1, to=5, length=200, resolution=1, command=zoom_img)
scale.place(x=85, y=140)

def move_from(event):
    canvas.scan_mark(event.x, event.y)

def move_to(event):
    canvas.scan_dragto(event.x, event.y, gain=1)


您正在尝试使用 canvas 扫描功能,该功能仅适用于 canvas 个对象。您不是在创建 canvas 对象,而是在创建标签并将它们放在 canvas 和 pack 上。您将无法滚动或扫描使用 packplacegrid.

添加到 canvas 的任何内容

为了能够使用扫描功能,您必须使用 canvas' create_image 方法使图像成为 canvas 上的对象。

这里有一个例子展示了如何使用 canvas 的扫描功能。注意:扫描功能不会移动图像,它会以与滚动条相同的方式移动视口。图片的实际坐标不变。

import tkinter as tk

def main():
    root = tk.Tk()

    image = tk.PhotoImage(data = image_data)

    canvas = tk.Canvas(root, width=400, height=400, bg="white")
    canvas.pack(fill="both", expand=True)

    canvas.create_image(10, 10, anchor="nw", image=image)

    canvas.bind("<ButtonPress-1>", drag_start)
    canvas.bind("<B1-Motion>", drag_move)

    root.mainloop()

def drag_start(event):
    event.widget.scan_mark(event.x, event.y)

def drag_move(event):
    event.widget.scan_dragto(event.x, event.y, gain=1)

image_data = """
    R0lGODlhGAAYAPZvAAIaLgwmOwcnQQcoQBMxSBc4VBQ6Vhk7VRY9WhY+XSI4RxJC
    ZRRCZi9KYSFNbzJUbSJOcCZVeiZUeyBWfy1Zei5afB1Wgh1XgyFXgDpjgSVjkyZl
    liZnmCxrmixsmjZtlSptoCpuoSxxqCxzqjh1oTV3qDR4qD5/rjV8tDV+tUFlgUZp
    hFNwhVVyh0ZzlUh7nnB8hUF8pkJ8pkx+oTuBuFmBn3SEkHeOn3mNnUeBqUiBqUqD
    q1CBpFKCpVuDoVyEokeGs0eHs0aKvkaMvlGOuGqKom+Pp3yUpziEwTiFwz2KxT+K
    xE6VylKUw1qaxV6fx0CQ0E+b1lCd1l6h0Wiq1Wyu12Wp22aq3FOi4F2s5WCt5W64
    7XW24Xu743u/6omfsZymroqhso6jtJynsJ2psaS1w6S2xIvH7YvI7d/h5ODi5eDk
    5+Dm6uLn7OPo7QAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAACH+SUNvcHlyaWdodCBJTkNPUlMgR21iSCAod3d3Lmljb25l
    eHBlcmllbmNlLmNvbSkgLSBVbmxpY2Vuc2VkIHByZXZpZXcgaW1hZ2UAIfkEBQAA
    bwAsAAAAABgAGAAAB9eAb4KDhIWGh4iJiouMjY6PiGkwCgAACjBqj2ABBSw3Ny0H
    AWONYwIqYmFfX2FiKwNki2kEFEZFRba3RhUEa4o2Cy41NT/FPsMuDDaKDRYfLzM8
    PT0zMy8fFw2KBRoeJDI6Ojs5MSQdGwaKCCAmJ0FE8ERAJyUhCYoPIjRDTU5PT06a
    CKEx4oEiHBuWMJlCpUoVKlOYKOFwQxEbB0ikXOHSpQsXK1GSQGizyIwFKFq8oDnj
    JQsUDGYalYmAYsqWLVhSSIjpqM2RDBMuZDjiBpLRo0iTKjUaCAA7
"""

if __name__ == "__main__":
    main()