From 44c020324a4d3a43545f761d292ede52d7b1b5b4 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Sat, 3 Jul 2021 14:25:52 -0400 Subject: [PATCH 1/2] New Demo - Combo File Chooser with history and clear button. --- ...ombo_Filechooser_With_History_And_Clear.py | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 DemoPrograms/Demo_Combo_Filechooser_With_History_And_Clear.py diff --git a/DemoPrograms/Demo_Combo_Filechooser_With_History_And_Clear.py b/DemoPrograms/Demo_Combo_Filechooser_With_History_And_Clear.py new file mode 100644 index 00000000..394cf359 --- /dev/null +++ b/DemoPrograms/Demo_Combo_Filechooser_With_History_And_Clear.py @@ -0,0 +1,44 @@ +import PySimpleGUI as sg + +""" + Demo Combo File Chooser - with clearable history + + This is a design pattern that is very useful for programs that you run often that requires + a filename be entered. You've got 4 options to use to get your filename with this pattern: + 1. Copy and paste a filename into the combo element + 2. Use the last used item which will be visible when you create the window + 3. Choose an item from the list of previously used items + 4. Browse for a new name + + To clear the list of previous entries, click the "Clear History" button. + + The history is stored in a json file using the PySimpleGUI User Settings APIs + + The code is as sparse as possible to enable easy integration into your code. + + Copyright 2021 PySimpleGUI +""" + + + +layout = [[sg.Combo(sg.user_settings_get_entry('-filenames-', []), default_value=sg.user_settings_get_entry('-last filename-', ''), size=(50, 1), key='-FILENAME-'), sg.FileBrowse(), sg.B('Clear History')], + [sg.Button('Ok'), sg.Button('Cancel')]] + +window = sg.Window('Filename Chooser With History', layout) + +while True: + event, values = window.read() + + if event in (sg.WIN_CLOSED, 'Cancel'): + break + if event == 'Ok': + # If OK, then need to add the filename to the list of files and also set as the last used filename + sg.user_settings_set_entry('-filenames-', list(set(sg.user_settings_get_entry('-filenames-', []) + [values['-FILENAME-'], ]))) + sg.user_settings_set_entry('-last filename-', values['-FILENAME-']) + break + elif event == 'Clear History': + sg.user_settings_set_entry('-filenames-', []) + sg.user_settings_set_entry('-last filename-', '') + window['-FILENAME-'].update(values=[], value='') + +window.close() From e4125886a3727a5e13698270764a4bfcf1395253 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Sat, 3 Jul 2021 14:29:14 -0400 Subject: [PATCH 2/2] Sort the list of entries --- DemoPrograms/Demo_Combo_Filechooser_With_History_And_Clear.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DemoPrograms/Demo_Combo_Filechooser_With_History_And_Clear.py b/DemoPrograms/Demo_Combo_Filechooser_With_History_And_Clear.py index 394cf359..3ba65023 100644 --- a/DemoPrograms/Demo_Combo_Filechooser_With_History_And_Clear.py +++ b/DemoPrograms/Demo_Combo_Filechooser_With_History_And_Clear.py @@ -21,7 +21,7 @@ import PySimpleGUI as sg -layout = [[sg.Combo(sg.user_settings_get_entry('-filenames-', []), default_value=sg.user_settings_get_entry('-last filename-', ''), size=(50, 1), key='-FILENAME-'), sg.FileBrowse(), sg.B('Clear History')], +layout = [[sg.Combo(sorted(sg.user_settings_get_entry('-filenames-', [])), default_value=sg.user_settings_get_entry('-last filename-', ''), size=(50, 1), key='-FILENAME-'), sg.FileBrowse(), sg.B('Clear History')], [sg.Button('Ok'), sg.Button('Cancel')]] window = sg.Window('Filename Chooser With History', layout)