0

I would like to create generic code to display images with an associated function. I tried this:

def open(url):
   print(url)

dictImage = {}
dictImage['imageA'] = {'var':'imgA' , 'path': 'imageA.png' , 'pathHtml':'imageA.html'}
dictImage['imageB'] = {'var':'imgB' , 'path': 'imageB.png' , 'pathHtml':'imageB.html'}

global variables
variables = {}
row = 0

for image in dictImage :
   path = Path(dictImage[image]['path'])
   imageTemp = ImageTk.PhotoImage(Image.open(path), Image.ANTIALIAS)
   imageTemp = ImageTk.PhotoImage(Image.open(path).resize((int(imageTemp.width()/1.3), 
   int(imageTemp.height()/1.3))
   variables[dictImage[image]['var']] = imageTemp

   panel = Label(frame, image= variables[dictImage[image]['var']])
   panel.grid(row=row, column=0)
   panel.bind("<Button-1>", lambda event: open(Path(dictImage[image]['path2']))

   label = Label(frame, text=dictImage[image]['var'])
   label.grid(row=row+1, column=0)

   row = row+2

But it doesn't work. (I use the pillow library) When I click on an image I cannot retrieve its url. It is always the url of the last displayed image that is taken.

martineau
  • 119,623
  • 25
  • 170
  • 301
poolpy
  • 31
  • 5
  • What does _"But it doesn't work"_ mean?is there any error or something, please include it – Delrius Euphoria Aug 26 '20 at 13:45
  • @CoolCloud I have not error but when I click on an image it's always the url of the last image which is considered for the open function – poolpy Aug 26 '20 at 13:58
  • How are `url` and the image related ?Also try saying `panel.image = variables[dictImage[image]['var']]` before `panel.grid(...)` – Delrius Euphoria Aug 26 '20 at 13:59
  • It's because the `lambda` function created references the value of `image`, which ends up being its final value after the `for image in dictImage:` loop finishes. The question is likely a dup… – martineau Aug 26 '20 at 14:51

0 Answers0