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