Reputation: 51
I've seen a couple posts similar to my question, but not exactly this, so apologies if this is a repeat question.
I'm using tkinter to read a folder, create a listbox that lists all the files (they're all going to be .tif files), and the user selects the image they want to view. Unfortunately files will be added and removed so I would like to update my listbox automatically. Here's some of the code I have so far:
from tkinter import *
from PIL import Image, ImageTk
import os
file_path = "C:/Users/USX27512/Desktop/Python UI/Test_TIFFs"
root = Tk()
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
os.chdir(file_path)
self.grid()
self.canv = Canvas(self, relief=SUNKEN)
self.canv.grid(row=2, column=1, columnspan=5, sticky=N)
self.canv.config(width=500, height=500)
self.canv.config(highlightthickness=0, background="black")
self.view_files()
def view_files(self):
lb = Listbox(self, height=20)
lb.update_idletasks()
files = sorted(os.listdir(file_path), key=lambda x: os.path.getctime(os.path.join(file_path, x)))
for file in files:
if os.path.isfile(os.path.join(file_path, file)):
lb.insert(END, file)
lb.bind("<Double-Button-1>", self.on_double)
lb.grid(row=2, column=0, sticky=NW)
def on_double(self, event):
widget = event.widget
selection = widget.curselection()
value = widget.get(selection[0])
sbarV = Scrollbar(self, orient=VERTICAL)
sbarH = Scrollbar(self, orient=HORIZONTAL)
sbarV.config(command=self.canv.yview)
sbarH.config(command=self.canv.xview)
self.canv.config(yscrollcommand=sbarV.set)
self.canv.config(xscrollcommand=sbarH.set)
sbarV.grid(row=2, column=6, sticky=N+S)
sbarH.grid(row=3, column=1, columnspan= 5, sticky=E+W)
self.im = Image.open(value)
image_width, image_height = self.im.size
self.canv.config(scrollregion=(0, 0, image_width, image_height))
self.im2 = ImageTk.PhotoImage(self.im)
self.imgtag = self.canv.create_image(0,0, anchor=NW, image=self.im2)
app = Application(root)
root.mainloop()
Any suggestions?
Upvotes: 0
Views: 5437