0

what is happening is that the showChoice function is creating a Label with a respective image. It runs in a loop until it shows all the images that the user has chosen from the Windows browser. What I'm looking for is to be able to do an event with .bind() for that label that is created in that function WITHOUT assigning a variable to the labels, because I'm looking to optimize as much code as possible. I would like to know if it is possible or if I have to assign a variable name to each label created. (tried to use lists and dictionaries but it didn't work for me).

some things are written in spanish but it is understood :)


def showChoice(rows,columns,count,files):
    print(columns)
    print(rows)
    print("cuenta= ",count)

    load=Image.open(files[count])

    razon_cambio=0
    h = load.height
    w = load.width

    if h > w:
        razon_cambio=h/200
        n_heigth=h/razon_cambio
        n_width=w/razon_cambio
        imagenR = load.resize((int(n_width),int(n_heigth)))
    else:
        razon_cambio=w/200
        n_heigth=h/razon_cambio
        n_width=w/razon_cambio
        imagenR = load.resize((int(n_width),int(n_heigth)))

    render.append(ImageTk.PhotoImage(imagenR))
    #Label(text=f"Imagen{count}",padx=10,pady=10).grid(row=rows,column=columns)
    
    labels_img={}
    labels_img[f"Label {count}"]=Label(frame,image=render[count],padx=10,pady=10).grid(row=rows,column=columns)
    labels_img[f"Label {count}"].bind('<Button>',print("hola"))
    
    #Label.grid(row=rows,column=columns)"""   
    

def choice():
    global render
    render=[]

    files=askopenfilenames()
    if not files:
        return
    elif len(files)>10:
        showerror(title="Error", message="Error, solo se puede cargar un máximo de 10 imágenes a la vez")
    else:
        count=0
        for row in range(3):
            for columns in range(4):
                if count >= len(files):
                    print("Tamaño del render",render)
                    return  
                showChoice(row,columns,count,files)
                count+=1
    

def salir():
    root.destroy()


barraMenu=Menu(root)
root.config(menu=barraMenu)


archivoMenu=Menu(barraMenu, tearoff=0)
barraMenu.add_cascade(label="Archivos", menu=archivoMenu)

archivoMenu.add_command(label="Abrir carpeta")
archivoMenu.add_command(label="Abrir archivo/s", command=choice)
archivoMenu.add_separator()
archivoMenu.add_command(label="Salir", command=salir)

tried to use lists and dictionaries but it didn't work for me

  • `x = Label(...).grid(...)` will assign the result of `.grid(...)` to `x` which is `None`. You need to put `.grid(...)` in separate line `x.grid(...)`. For this case, you need a variable to create the label. – acw1668 Dec 03 '22 at 01:37
  • the code works normal, but i would like to know if you can assign a bind event without having to assign a variable to each label. it can't be done? I precisely want to avoid creating the variables because I would have to make 10 in my case. My intention is to save code – Cesar Alejandro Perez Ochoa Dec 03 '22 at 01:44
  • I have already said *"For this case, you need a variable to create the label."* – acw1668 Dec 03 '22 at 01:48

0 Answers0