|
| 1 | +import PySimpleGUI as sg |
| 2 | + |
| 3 | +""" |
| 4 | + Demo of a Better File / Folder Input Window |
| 5 | +
|
| 6 | + This construct is very common in PySimpleGUI. |
| 7 | + [sg.InputText(size=(50,1), key='-FILENAME-'), sg.FileBrowse()], |
| 8 | +
|
| 9 | + The new user settings APIs can significantly improve the experience. Now instead of being |
| 10 | + shown a blank input element, the user is shown their previous entry and a history of their |
| 11 | + prior entries to choose from. |
| 12 | +
|
| 13 | + Two new capabilities are presented in this demo |
| 14 | + 1. Recalling the last entry |
| 15 | + 2. Recalling a history of all of the previous entries as a Combo instead of Input Element |
| 16 | +
|
| 17 | + The previous operations you're used to remain. You can paste a filename/full path into the combo. |
| 18 | + You can also use the browse button as before. |
| 19 | +
|
| 20 | + But, you also get the 2 history features - last entry used, list of previous choices. |
| 21 | +
|
| 22 | + If your window is not a 1-shot, add an event loop instead of a read with close paramter |
| 23 | +
|
| 24 | + Copyright 2020 PySimpleGUI.org |
| 25 | +""" |
| 26 | + |
| 27 | +# ------------------- The Old Way ------------------- |
| 28 | + |
| 29 | +layout = [[sg.Text('My Window')], |
| 30 | + [sg.InputText(size=(50, 1), key='-FILENAME-'), sg.FileBrowse()], |
| 31 | + [sg.Button('Go'), sg.Button('Exit')]] |
| 32 | + |
| 33 | +event1, values1 = sg.Window('Window Title', layout).read(close=True) |
| 34 | + |
| 35 | +# ------------------- The New Way with history ------------------- |
| 36 | + |
| 37 | +layout = [[sg.Text('My Window')], |
| 38 | + [sg.Combo(sg.user_settings_get_entry('-filenames-', []), default_value=sg.user_settings_get_entry('-last filename-', ''), size=(50, 1), key='-FILENAME-'), |
| 39 | + sg.FileBrowse()], |
| 40 | + [sg.Button('Go'), sg.Button('Exit')]] |
| 41 | + |
| 42 | +event, values = sg.Window('Window Title', layout).read(close=True) |
| 43 | + |
| 44 | +if event == 'Go': |
| 45 | + sg.user_settings_set_entry('-filenames-', list(set(sg.user_settings_get_entry('-filenames-', []) + [values['-FILENAME-'], ]))) |
| 46 | + sg.user_settings_set_entry('-last filename-', values['-FILENAME-']) |
| 47 | + |
| 48 | +# ------------------- The New Way with history and clear ------------------- |
| 49 | + |
| 50 | +layout = [[sg.Text('My Window')], |
| 51 | + [sg.Combo(sg.user_settings_get_entry('-filenames-', []), default_value=sg.user_settings_get_entry('-last filename-', ''), size=(50, 1), key='-FILENAME-'), |
| 52 | + sg.FileBrowse()], |
| 53 | + [sg.Button('Go'), sg.B('Clear'), sg.Button('Exit')]] |
| 54 | + |
| 55 | +window = sg.Window('Window Title', layout) |
| 56 | + |
| 57 | +while True: |
| 58 | + event, values = window.read() |
| 59 | + |
| 60 | + if event == sg.WIN_CLOSED or event == 'Exit': |
| 61 | + break |
| 62 | + if event == 'Go': |
| 63 | + sg.user_settings_set_entry('-filenames-', list(set(sg.user_settings_get_entry('-filenames-', []) + [values['-FILENAME-'], ]))) |
| 64 | + sg.user_settings_set_entry('-last filename-', values['-FILENAME-']) |
| 65 | + window['-FILENAME-'].update(values=list(set(sg.user_settings_get_entry('-filenames-', [])))) |
| 66 | + elif event == 'Clear': |
| 67 | + sg.user_settings_set_entry('-filenames-', []) |
| 68 | + sg.user_settings_set_entry('-last filename-', '') |
| 69 | + window['-FILENAME-'].update(values=[], value='') |
0 commit comments