Skip to content

Instantly share code, notes, and snippets.

@driscollis
Created May 5, 2020 16:36
Show Gist options
  • Save driscollis/e3ff2ce849126b9398572410df95dd2d to your computer and use it in GitHub Desktop.
Save driscollis/e3ff2ce849126b9398572410df95dd2d to your computer and use it in GitHub Desktop.
import PySimpleGUI as sg
import os.path
# --------------------------------- Define Layout ---------------------------------
# First the window layout...2 columns
left_col = [
[
sg.Text("Folder"),
sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
sg.FolderBrowse(),
],
[sg.Listbox(values=[], enable_events=True, size=(40, 20), key="-FILE LIST-")],
]
# For now will only show the name of the file that was chosen
images_col = [
[sg.Text("You choose from the list:")],
[sg.Text(size=(40, 1), key="-TOUT-")],
[sg.Image(key="-IMAGE-")],
]
# ----- Full layout -----
layout = [[sg.Column(left_col), sg.VSeperator(), sg.Column(images_col)]]
# --------------------------------- Create Window ---------------------------------
window = sg.Window("Image Viewer", layout)
# ----- Run the Event Loop -----
# --------------------------------- Event Loop ---------------------------------
while True:
event, values = window.read()
if event in (None, "Exit"):
break
if (
event == "-FOLDER-"
): # Folder name was filled in, make a list of files in the folder
folder = values["-FOLDER-"]
try:
file_list = os.listdir(folder) # get list of files in folder
except:
file_list = []
fnames = [
f
for f in file_list
if os.path.isfile(os.path.join(folder, f))
and f.lower().endswith((".png", ".jpg", "jpeg", ".tiff", ".bmp"))
]
window["-FILE LIST-"].update(fnames)
elif event == "-FILE LIST-": # A file was chosen from the listbox
try:
filename = os.path.join(values["-FOLDER-"], values["-FILE LIST-"][0])
window["-TOUT-"].update(filename)
window["-IMAGE-"].update(filename=filename)
except:
pass # something weird happened making the full filename
# --------------------------------- Close & Exit ---------------------------------
window.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment