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