我有两张完全相同大小的图像。一个是基本背景图像,另一个图像是背景图像的更改版本。我试图在背景图像的顶部显示一个静态大小(例如 100,100)的已更改图像的窗口,无论鼠标移动到哪里,它们都会在其中排列。一种将更改后的图像覆盖在基础图像上的移动窗口。我一直在尝试修改我在 Stack Overflow 上某处找到的一些基本代码,但似乎无法弄清楚如何修改它以获得所需的结果。如果您认为我以错误的方式解决此问题,或者如果我错过了某个类似的示例,请建议另一种方法。我对 tkinter 很陌生。此外,我不需要下面的图像示例中显示的坐标系。光是画面就很好。
from tkinter import *
from PIL import ImageTk, Image
from scipy.misc import imread
event2canvas = lambda e, c: (c.canvasx(e.x), c.canvasy(e.y))
# function to be called when mouse is moved
def move_window(event):
# outputting x and y coords to console
cx, cy = event2canvas(event, canvas)
print("(%d, %d) / (%d, %d)" % (event.x, event.y, cx, cy))
if __name__ == "__main__":
root = Tk()
#setting up a tkinter canvas with scrollbars
frame = Frame(root, bd=2, relief=SUNKEN)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
xscroll = Scrollbar(frame, orient=HORIZONTAL)
xscroll.grid(row=1, column=0, sticky=E+W)
yscroll = Scrollbar(frame)
yscroll.grid(row=0, column=1, sticky=N+S)
canvas = Canvas(frame, bd=0, xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
canvas.grid(row=0, column=0, sticky=N+S+E+W)
xscroll.config(command=canvas.xview)
yscroll.config(command=canvas.yview)
frame.pack(fill=BOTH,expand=1)
# loading background and altered image into numpy array.
background_image_data = imread("images/original.png", mode="L")
foreground_image_data = imread("images/altered.png", mode="L")
# Here is where the mouse coordinates should be injected to move the window over the background image
window_data = foreground_image_data[500:600, 500:600]
background_image_data[500:600, 500:600] = window_data
img = ImageTk.PhotoImage(Image.fromarray(background_image_data))
canvas.create_image(0, 0,image=img,anchor="nw")
canvas.config(scrollregion=canvas.bbox(ALL), width=img.width(), height=img.height())
test = canvas.bind("<ButtonPress-1>", move_window)
root.mainloop()