MCPcopy
hub / github.com/PySimpleGUI/PySimpleGUI / main

Function main

DemoPrograms/Demo_PNG_Viewer.py:16–94  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

14
15
16def main():
17
18 # Get the folder containing the images from the user
19 folder = sg.popup_get_folder('Image folder to open')
20 if folder is None:
21 sg.popup_cancel('Cancelling')
22 return
23
24 # get list of PNG files in folder
25 png_files = [os.path.join(folder, f) for f in os.listdir(folder) if f.lower().endswith('.png')]
26 filenames_only = [f for f in os.listdir(folder) if f.lower().endswith('.png')]
27
28 if len(png_files) == 0:
29 sg.popup('No PNG images in folder')
30 return
31
32 # define menu layout
33 menu = [['File', ['Open Folder', 'Exit']], ['Help', ['About', ]]]
34
35 # define layout, show and read the window
36 col = [[sg.Text(png_files[0], size=(80, 3), key='-FILENAME-')],
37 [sg.Image(filename=png_files[0], key='-IMAGE-', expand_x=True, expand_y=True)],
38 [sg.Button('Next', size=(8, 2)), sg.Button('Prev', size=(8, 2)),
39 sg.Text('File 1 of {}'.format(len(png_files)), size=(15, 1), key='-FILENUM-')]]
40
41 col_files = [[sg.Listbox(values=filenames_only, size=(60, 30), key='-LISTBOX-', enable_events=True)],
42 [sg.Text('Select a file. Use scrollwheel or arrow keys on keyboard to scroll through files one by one.')]]
43
44 layout = [[sg.Menu(menu)], [sg.Col(col_files), sg.Col(col, expand_x=True, expand_y=True)]]
45
46 window = sg.Window('Image Browser', layout, return_keyboard_events=True, use_default_focus=False)
47
48 # loop reading the user input and displaying image, filename
49 filenum, filename = 0, png_files[0]
50 while True:
51
52 event, values = window.read()
53 # --------------------- Button & Keyboard ---------------------
54 if event == sg.WIN_CLOSED:
55 break
56 elif event in ('Next', 'MouseWheel:Down', 'Down:40', 'Next:34') and filenum < len(png_files)-1:
57 filenum += 1
58 filename = os.path.join(folder, filenames_only[filenum])
59 window['-LISTBOX-'].update(set_to_index=filenum, scroll_to_index=filenum)
60 elif event in ('Prev', 'MouseWheel:Up', 'Up:38', 'Prior:33') and filenum > 0:
61 filenum -= 1
62 filename = os.path.join(folder, filenames_only[filenum])
63 window['-LISTBOX-'].update(set_to_index=filenum, scroll_to_index=filenum)
64 elif event == 'Exit':
65 break
66 elif event == '-LISTBOX-':
67 filename = os.path.join(folder, values['-LISTBOX-'][0])
68 filenum = png_files.index(filename)
69 # ----------------- Menu choices -----------------
70 if event == 'Open Folder':
71 newfolder = sg.popup_get_folder('New folder', no_window=True)
72 if newfolder is None:
73 continue

Callers 1

Demo_PNG_Viewer.pyFile · 0.70

Calls 4

readMethod · 0.95
refreshMethod · 0.95
closeMethod · 0.95
updateMethod · 0.45

Tested by

no test coverage detected