pysimplegui image viewer

Solutions on MaxInterview for pysimplegui image viewer by the best coders in the world

showing results for - "pysimplegui image viewer"
Michelle
11 Nov 2017
1import PySimpleGUI as sg
2import os
3
4# Simple Image Browser based on PySimpleGUI
5
6# Get the folder containing the images from the user
7rc, folder = sg.GetPathBox('Image Browser', 'Image folder to open', default_path='X:/- EBAY - Gruen/Images - Oct Nov Dec 2017')
8if rc is False or folder is '':
9    sg.MsgBoxCancel('Cancelling')
10    exit(0)
11
12# get list of PNG files in folder
13png_files = [folder + '\\' + f for f in os.listdir(folder) if '.png' in f]
14filenames_only = [f for f in os.listdir(folder) if '.png' in f]
15
16if len(png_files) == 0:
17    sg.MsgBox('No PNG images in folder')
18    exit(0)
19
20# create the form that also returns keyboard events
21form = sg.FlexForm('Image Browser', return_keyboard_events=True, location=(0,0), use_default_focus=False )
22
23# make these 2 elements outside the layout because want to "update" them later
24# initialize to the first PNG file in the list
25image_elem = sg.Image(filename=png_files[0])
26filename_display_elem = sg.Text(png_files[0], size=(80, 3))
27file_num_display_elem = sg.Text('File 1 of {}'.format(len(png_files)), size=(15,1))
28
29# define layout, show and read the form
30col = [[filename_display_elem],
31          [image_elem],
32          [sg.ReadFormButton('Next', size=(8,2)), sg.ReadFormButton('Prev', size=(8,2)), file_num_display_elem]]
33
34col_files = [[sg.Listbox(values=filenames_only, size=(60,30), key='listbox')],
35             [sg.ReadFormButton('Read')]]
36layout = [[sg.Column(col_files), sg.Column(col)]]
37button, values = form.LayoutAndRead(layout)          # Shows form on screen
38
39# loop reading the user input and displaying image, filename
40i=0
41while True:
42
43    # perform button and keyboard operations
44    if button is None:
45        break
46    elif button in ('Next', 'MouseWheel:Down', 'Down:40', 'Next:34') and i < len(png_files)-1:
47        i += 1
48    elif button in ('Prev', 'MouseWheel:Up', 'Up:38', 'Prior:33') and i > 0:
49        i -= 1
50
51    if button == 'Read':
52        filename = folder + '\\' + values['listbox'][0]
53        # print(filename)
54    else:
55        filename = png_files[i]
56
57    # update window with new image
58    image_elem.Update(filename=filename)
59    # update window with filename
60    filename_display_elem.Update(filename)
61    # update page display
62    file_num_display_elem.Update('File {} of {}'.format(i+1, len(png_files)))
63
64    # read the form
65    button, values = form.Read()
66
67