Merge pull request #4971 from PySimpleGUI/Dev-latest

Refresh of demo to use coding conventions for keys, make portable fil…
This commit is contained in:
PySimpleGUI 2021-11-19 14:35:09 -05:00 committed by GitHub
commit 717940abb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 31 deletions

View File

@ -3,7 +3,11 @@ import PySimpleGUI as sg
import os import os
''' '''
Simple Image Browser based on PySimpleGUI Simple Image Browser
This is an early demo program, so perhaps not quite as sophisticated as later ones.
Copyright 2021 PySimpleGUI
''' '''
@ -16,8 +20,8 @@ def main():
return return
# get list of PNG files in folder # get list of PNG files in folder
png_files = [folder + '\\' + f for f in os.listdir(folder) if '.png' in f] png_files = [os.path.join(folder, f) for f in os.listdir(folder) if f.lower().endswith('.png')]
filenames_only = [f for f in os.listdir(folder) if '.png' in f] filenames_only = [f for f in os.listdir(folder) if f.lower().endswith('.png')]
if len(png_files) == 0: if len(png_files) == 0:
sg.popup('No PNG images in folder') sg.popup('No PNG images in folder')
@ -27,39 +31,39 @@ def main():
menu = [['File', ['Open Folder', 'Exit']], ['Help', ['About', ]]] menu = [['File', ['Open Folder', 'Exit']], ['Help', ['About', ]]]
# define layout, show and read the window # define layout, show and read the window
col = [[sg.Text(png_files[0], size=(80, 3), key='filename')], col = [[sg.Text(png_files[0], size=(80, 3), key='-FILENAME-')],
[sg.Image(filename=png_files[0], key='image')], [sg.Image(filename=png_files[0], key='-IMAGE-')],
[sg.Button('Next', size=(8, 2)), sg.Button('Prev', size=(8, 2)), [sg.Button('Next', size=(8, 2)), sg.Button('Prev', size=(8, 2)),
sg.Text('File 1 of {}'.format(len(png_files)), size=(15, 1), key='filenum')]] sg.Text('File 1 of {}'.format(len(png_files)), size=(15, 1), key='-FILENUM-')]]
col_files = [[sg.Listbox(values=filenames_only, size=(60, 30), key='-LISTBOX-', enable_events=True)],
[sg.Text('Select a file. Use scrollwheel or arrow keys on keyboard to scroll through files one by one.')]]
col_files = [[sg.Listbox(values=filenames_only, size=(60, 30), key='listbox')],
[sg.Button('Read')]]
layout = [[sg.Menu(menu)], [sg.Col(col_files), sg.Col(col)]] layout = [[sg.Menu(menu)], [sg.Col(col_files), sg.Col(col)]]
window = sg.Window('Image Browser', layout,
return_keyboard_events=True, window = sg.Window('Image Browser', layout, return_keyboard_events=True, use_default_focus=False)
location=(0, 0),
use_default_focus=False)
# loop reading the user input and displaying image, filename # loop reading the user input and displaying image, filename
i = 0 filenum, filename = 0, png_files[0]
while True: while True:
event, values = window.read() event, values = window.read()
# --------------------- Button & Keyboard --------------------- # --------------------- Button & Keyboard ---------------------
if event == sg.WIN_CLOSED: if event == sg.WIN_CLOSED:
break break
elif event in ('Next', 'MouseWheel:Down', 'Down:40', 'Next:34') and i < len(png_files)-1: elif event in ('Next', 'MouseWheel:Down', 'Down:40', 'Next:34') and filenum < len(png_files)-1:
i += 1 filenum += 1
elif event in ('Prev', 'MouseWheel:Up', 'Up:38', 'Prior:33') and i > 0: filename = os.path.join(folder, filenames_only[filenum])
i -= 1 window['-LISTBOX-'].update(set_to_index=filenum, scroll_to_index=filenum)
elif event in ('Prev', 'MouseWheel:Up', 'Up:38', 'Prior:33') and filenum > 0:
filenum -= 1
filename = os.path.join(folder, filenames_only[filenum])
window['-LISTBOX-'].update(set_to_index=filenum, scroll_to_index=filenum)
elif event == 'Exit': elif event == 'Exit':
break break
elif event == '-LISTBOX-':
if event == 'Read': filename = os.path.join(folder, values['-LISTBOX-'][0])
filename = folder + '/' + values['listbox'][0] filenum = png_files.index(filename)
else:
filename = png_files[i]
# ----------------- Menu choices ----------------- # ----------------- Menu choices -----------------
if event == 'Open Folder': if event == 'Open Folder':
newfolder = sg.popup_get_folder('New folder', no_window=True) newfolder = sg.popup_get_folder('New folder', no_window=True)
@ -67,24 +71,23 @@ def main():
continue continue
folder = newfolder folder = newfolder
png_files = [folder + '/' + png_files = [os.path.join(folder, f) for f in os.listdir(folder) if f.lower().endswith('.png')]
f for f in os.listdir(folder) if '.png' in f] filenames_only = [f for f in os.listdir(folder) if f.lower().endswith('.png')]
filenames_only = [f for f in os.listdir(folder) if '.png' in f]
window['listbox'].update(values=filenames_only) window['-LISTBOX-'].update(values=filenames_only)
window.refresh() window.refresh()
i = 0 filenum = 0
elif event == 'About': elif event == 'About':
sg.popup('Demo PNG Viewer Program', sg.popup('Demo PNG Viewer Program',
'Please give PySimpleGUI a try!') 'Please give PySimpleGUI a try!')
# update window with new image # update window with new image
window['image'].update(filename=filename) window['-IMAGE-'].update(filename=filename)
# update window with filename # update window with filename
window['filename'].update(filename) window['-FILENAME-'].update(filename)
# update page display # update page display
window['filenum'].update('File {} of {}'.format(i+1, len(png_files))) window['-FILENUM-'].update('File {} of {}'.format(filenum + 1, len(png_files)))
window.close() window.close()