Home > front end >  How to pass arguments when instantiating a class to a function in Python?
How to pass arguments when instantiating a class to a function in Python?

Time:10-05

I have tried to portray and document each class only what is necessary, hope you can help me please, I am trying to instantiate the class Show_image which requires two parameters, two indexes of a list of images, if you could guide me if what I am trying to do is possible I would appreciate it. Thanks.

This class resizes the image

from tkinter importante *
from PIL import Image, ImageTk

class Array(Frame):
    def __init__(self, master, path, *args):
        Frame.__init__(self, master, *args)       self.image = Image.open(path)
        self.img_copy = self.image.copy()

        self.background_image = ImageTk.PhotoImage(self.image)

        self.background = Label(self, image=self.background_image)
        self.background.pack(fill='both', expand=True)
        self.background.bind('<Configure>', self._resize_image)

    def _resize_image(self, event):
        self.image = self.img_copy .resize ((self.master .winfo_width(), self.master .winfo_height()))

        self.background_image = ImageTk.PhotoImage(self.image)
        self.background.configure(image=self.background_image)

Base class control

class Interface(Frame):
    def __init__(self, master, *args):
        Frame.__init__(self, master, *args)
        self.path_lst = ['11.png','22.png','33.png','44.png']  # change/add paths 
        self._frame_1 = None
        self._open_1 = False
        self.button1 = Button(self, text='pack 1',
                      command= lambda:self.windows(Show_image))
        self.button1 .pack()
 
    def windows(self, var_1):
        if not self._open_1:
            self.top1 = Toplevel(self.master)
            self.top1 .geometry('200x200')
                                
        container = var_1(self.top1)

        if self._frame_1 is not None:  
            self._frame_1 .destroy()
        self._frame_1 = container
        self._frame_1 .pack()
        self._open_1 = True

        self.top1.protocol ('WM_DELETE_WINDOW', lambda: self.closed(1))
    
    def closed(self, number):
        if number == 1:
            self.top1. destroy()
            self._open_1 = False


This class should draw two instances, two images, but I don't know how to pass <index_1> and <index_2> arguments in bound <lambda> function.

class Show_image(Frame):
    def __init__(self, index_1, index_2, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)
        self.img = Array(self, self.master.path_lst[index_1])
        self.img .grid(column=0, row=0)
        self.img2 = Array(self, self.master.path_lst[index_2])
        self.img2 .grid(column=0, row=1)

root = Tk()
frm = Interface(root)
frm .pack()
root.mainloop()

Also, if you could tell me if the initialization path of the images is correct. Thanks.

CodePudding user response:

If I understant you will have to use nested lambdas

command=lambda:self.windows(lambda:Show_image(index1, index2))

but I see that windows runs function with argument top so it will need it also in lambda

command=lambda:self.windows( lambda top:Show_image(index1, index2, top))
  • Related