Python Tkinter show image acessed on a listbox -
i'm trying make python script shows image acessed on listbox. code got on internet works:
import tkinter tk 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 tried adapt listbox stopped working.
from tkinter import * 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() 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()
what missing?
you must use configure
method of label, , store reference image somewhere.
self.c.image = img # save reference self.c.configure(image=img) # configure label
Comments
Post a Comment