python – Embed a mini-window in a resizable canvas

I have a resizable canvas, and I want to add a mini-window that I obtained via a previous question.

The code is too long to be shared, that’s why I’m sharing snippets of code with you

class ResizableCanvas(Canvas):

    def __init__(self, parent, **kwargs):
        Canvas.__init__(self, parent, **kwargs)
        self.bind("<Configure>", self.on_resize)
        self.height = self.winfo_reqheight()
        self.width = self.winfo_reqwidth()

    def on_resize(self, event):
        wscale = float(event.width) / self.width
        hscale = float(event.height) / self.height
        self.width = event.width
        self.height = event.height
        self.config(width=self.width, height=self.height)


class ViewerTab:
    def __init__(self, master, quit_import, dim=1000):

    
        self.coords = {"x": 0, "y": 0, "x2": 0, "y2": 0}

        self.lines = ()
        self.oval = ()
        self.rectangle = ()
        self.arc = ()

        self.canvas = ResizableCanvas(self.master,
                                      width=dim,
                                      height=dim,
                                      highlightthickness=0,
                                      bg="lavender")
        self.canvas.pack(fill=BOTH, expand=YES)
        

        self.canvas.bind("<Button-1>", self.dirbutton)
        self.canvas.bind("<B1-Motion>", self.move)
        self.canvas.bind("<ButtonRelease-1>", self.nomove)
        self.canvas.bind("<Button-2>", self.get_position)
        self.canvas.bind('<Button-3>', self.xy)

    def initView(self):

        self.canvas.delete('all')

        self.image = self.initImage()

        self.angle = 0
        self.redraw()
        self.isSlideOn = True

    def initViewSuperposed(self):

        self.canvas.delete('all')


        self.image = self.initImage()
        self.cmap = self.initImagePng()
        self.cmap = PIL.Image.fromarray((self.cmap * 255).astype(numpy.uint8))
        self.isSuperposed = True
        self.isSlideOn = True
        self.set_labels()
        self.redrawSuperposed()


    def my_resize(self, size):
        needed_y , needed_x = size
        size_new_image = max((needed_x,needed_y))
        new_image = PIL.Image.new('RGBA',(size_new_image,size_new_image),color=(255,255,255,0))
        n = numpy.array(new_image)
        factor = (2**self.level)
        pixel_size = 1

        for key in self.positions.keys():
            if key != 'size_x' and key != 'size_y':
                xo = int( (key(0) *598) / factor)
                yo = int( (key(1) *598) /factor)


            if self.level > 7:
                pixel_size = 1

            if self.level == 7:
                pixel_size = 4

            if self.level == 6:
                pixel_size = 9

            if self.level == 5:
                pixel_size = 18

            if self.level == 4:
                pixel_size = 37

            if self.level == 3:
                pixel_size = 74

            elif self.level == 2:
                pixel_size = 149

            elif self.level == 1:
                pixel_size = 299

            elif self.level == 0:
                pixel_size = 598

            for i in range(pixel_size):
                for j in range(pixel_size):
                    x_good = xo + i
                    y_good = yo + j
                    if x_good > needed_x:
                        x_good = 0
                    if y_good > needed_y:
                        y_good = 0
                    n(x_good,y_good) = self.positions(key)

        new_image = PIL.Image.fromarray(n)
        
        return new_image



    def dirbutton(self, event):
        if self.mode.get() == "pan":
            if self.isSlideOn:
                if self.tool == "slide":
                    self.xref = event.x
                    self.yref = event.y
        if self.mode.get() == "annotate" or self.mode.get() == "line" :
            self.canvas.scan_mark(event.x, event.y)

    def move(self, event):
        if self.mode.get() == "pan":
            if self.isSlideOn:
                if self.tool == "slide":
                    dpx = (event.x - self.xref)
                    dpy = (event.y - self.yref)
                    self.canvas.delete("image")
                    self.canvas.create_image(-self.canvas.width + dpx,
                                            -self.canvas.height + dpy, anchor=NW,
                                            image=self.photoimage, tags="image")
            
        if self.mode.get() == "annotate" or self.mode.get() == "line" or self.mode.get() == "oval" or self.mode.get() == "rectangle" or self.mode.get() == "arc":
            self.canvas.scan_dragto(event.x, event.y, gain=1)
    
    def nomove(self, event):
        if self.mode.get() == "pan":
            if self.isSuperposed:
                if self.tool == "slide":
                    self.image = self.translateImage(self.xref,
                                                        self.yref,
                                                        event)
                    self.redrawSuperposed()
        
            if self.isSlideOn and self.isSuperposed == False:
                if self.tool == "slide":
                    self.image = self.translateImage(self.xref,
                                                        self.yref,
                                                        event)
                    self.redraw()
            
  
    def get_position(self, event):
        factory = (-1)*int(numpy.sin(numpy.radians(self.angle))) + int(numpy.cos(numpy.radians(self.angle)))
        factorx = int(numpy.sin(numpy.radians(self.angle))) + int(numpy.cos(numpy.radians(self.angle)))*(-1)**(self.angle/90)
        if self.flip:
            event.x = self.canvas.width - event.x
        if self.angle % 180 == 0:
            abs_x = factorx*event.x + self.canvas.width*2**(self.angle/180) - self.cmapx
            abs_y = factory*event.y + self.canvas.height*2**(self.angle/180) - self.cmapy
        else:
            abs_x = factory*event.y + (3*self.canvas.width+self.canvas.height*(factorx))/2 - self.cmapx
            abs_y = factorx*event.x + (3*self.canvas.height+self.canvas.width*(factory))/2 - self.cmapy
        factor_resize_x = self.cmap_resize.size(0)/self.cmap_png.shape(0)
        factor_resize_y = self.cmap_resize.size(1)/self.cmap_png.shape(1)
        index_x = int(abs_x/factor_resize_x)
        index_y = int(abs_y/factor_resize_y)
        messagebox.showinfo('Patch coordinates', 'X: % d n Y: % d' % (index_x, index_y))

And the code I want to add is here:

from tkinter import Tk, Canvas
from PIL import Image, ImageTk
import requests

try:
    open('space.jpg')
except FileNotFoundError:
    url = 'https://images5.alphacoders.com/866/866360.jpg'
    img = requests.get(url)
    if img.status_code == 200:
        with open('space.jpg', 'wb') as file:
            file.write(img.content)
        print('File not found. Downloaded the necessary file.')
    else:
        print('File not found. Could not download the necessary file.')
        exit()


start_coords = {'x': 0, 'y': 0, 'move': False}

def set_start_coords(event):
    x1, y1, x2, y2 = nav_canvas.coords(nav_box)
    if x1 < event.x < x2 and y1 < event.y < y2:
        start_coords('x') = event.x - x1
        start_coords('y') = event.y - y1
        start_coords('move') = True
    else:
        start_coords('move') = False

def move_coords(event):
    if not start_coords('move'):
        return

    dx = start_coords('x')
    dy = start_coords('y')
    x = event.x - dx
    y = event.y - dy

    if x < 0:
        x = 0
    elif x + 70 > 200:
        x = 130
    if y < 0:
        y = 0
    elif y + 50 > 100:
        y = 50

    nav_canvas.coords(nav_box, x, y, x + 70, y + 50)
    main_canvas.coords(main_image_id, -x * 10, -y * 10)
root = Tk()
image = Image.open('space.jpg')
main_image = ImageTk.PhotoImage(image)
nav_image = ImageTk.PhotoImage(image.resize((200, 100), Image.ANTIALIAS))

main_canvas = Canvas(root, width=700, height=500, highlightthickness=0)
main_canvas.pack()
main_image_id = main_canvas.create_image((0, 0), image=main_image, anchor='nw')

nav_canvas = Canvas(width=200, height=100, highlightthickness=0)
main_canvas.create_window((500, 400), window=nav_canvas, anchor='nw', tags='nav_canvas')

nav_canvas.create_image((0, 0), image=nav_image, anchor='nw')
nav_box = nav_canvas.create_rectangle((0, 0, 70, 50), outline='white')

nav_canvas.bind('<Button-1>', set_start_coords)
nav_canvas.bind('<B1-Motion>', move_coords)

root.mainloop()