View jpg image dynamically in python Tkinter label

3

I'm trying to display a chosen image from a Listbox in a Label. The following code works:

import Tkinter as tk
from PIL import ImageTk, Image

window = tk.Tk()

path = 'img\2015722_univ_sqs_sm.jpg'
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(window, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")

window.mainloop()

But when I try to load the image from a listbox it does not work.

from Tkinter import *
from PIL import ImageTk, Image
import glob

files = glob.glob('img\*.jpg')

class App:

    def __init__(self, root):

        self.l = Listbox(root, width = 50, height = 15)
        self.l.pack()
        self.l.bind('<<ListboxSelect>>', self.lol)

        self.c = Label(root)
        self.c.pack()

        for f in files:
            self.l.insert(END, f)

    def lol(self, evt):

        path = files[self.l.curselection()[0]]
        img = ImageTk.PhotoImage(Image.open(path))
        self.c.image = img
        self.c.pack()

root = Tk()
App(root)
root.mainloop()

Where am I going wrong?

    
asked by anonymous 08.07.2015 / 18:34

1 answer

0

This snippet solved:

self.c.image = img           # save reference
self.c.configure(image=img)  # configure the label
    
09.07.2015 / 02:28