Advertisement
Najeebsk

IMAGE-VIEWER.py

Jan 11th, 2022
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. # IMAGE-VIEWER.py
  2.  
  3.  
  4. import PySimpleGUI as sg
  5.  
  6. import os.path
  7.  
  8.  
  9. # First the window layout in 2 columns
  10.  
  11.  
  12. file_list_column = [
  13.  
  14.     [
  15.  
  16.         sg.Text("Image Folder"),
  17.  
  18.         sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
  19.  
  20.         sg.FolderBrowse(),
  21.  
  22.     ],
  23.  
  24.     [
  25.  
  26.         sg.Listbox(
  27.  
  28.             values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
  29.  
  30.         )
  31.  
  32.     ],
  33.  
  34. ]
  35.  
  36.  
  37. # For now will only show the name of the file that was chosen
  38.  
  39. image_viewer_column = [
  40.  
  41.     [sg.Text("Choose an image from list on left:")],
  42.  
  43.     [sg.Text(size=(40, 1), key="-TOUT-")],
  44.  
  45.     [sg.Image(key="-IMAGE-")],
  46.  
  47. ]
  48.  
  49.  
  50. # ----- Full layout -----
  51.  
  52. layout = [
  53.  
  54.     [
  55.  
  56.         sg.Column(file_list_column),
  57.  
  58.         sg.VSeperator(),
  59.  
  60.         sg.Column(image_viewer_column),
  61.  
  62.     ]
  63.  
  64. ]
  65.  
  66.  
  67. window = sg.Window("Image Viewer", layout)
  68.  
  69.  
  70. # Run the Event Loop
  71.  
  72. while True:
  73.  
  74.     event, values = window.read()
  75.  
  76.     if event == "Exit" or event == sg.WIN_CLOSED:
  77.  
  78.         break
  79.  
  80.     # Folder name was filled in, make a list of files in the folder
  81.  
  82.     if event == "-FOLDER-":
  83.  
  84.         folder = values["-FOLDER-"]
  85.  
  86.         try:
  87.  
  88.             # Get list of files in folder
  89.  
  90.             file_list = os.listdir(folder)
  91.  
  92.         except:
  93.  
  94.             file_list = []
  95.  
  96.  
  97.         fnames = [
  98.  
  99.             f
  100.  
  101.             for f in file_list
  102.  
  103.             if os.path.isfile(os.path.join(folder, f))
  104.  
  105.             and f.lower().endswith((".png", ".gif"))
  106.  
  107.         ]
  108.  
  109.         window["-FILE LIST-"].update(fnames)
  110.  
  111.     elif event == "-FILE LIST-":  # A file was chosen from the listbox
  112.  
  113.         try:
  114.  
  115.             filename = os.path.join(
  116.  
  117.                 values["-FOLDER-"], values["-FILE LIST-"][0]
  118.  
  119.             )
  120.  
  121.             window["-TOUT-"].update(filename)
  122.  
  123.             window["-IMAGE-"].update(filename=filename)
  124.  
  125.  
  126.         except:
  127.  
  128.             pass
  129.  
  130.  
  131. window.close()
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement