popup_get_text - addition of history feature so at same level as other popup_get functions

This commit is contained in:
PySimpleGUI 2022-09-24 08:20:54 -04:00
parent 4ebc727679
commit 2482dff2d6
1 changed files with 92 additions and 45 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
version = __version__ = "4.60.3.98 Unreleased" version = __version__ = "4.60.3.99 Unreleased"
_change_log = """ _change_log = """
Changelog since 4.60.0 released to PyPI on 8-May-2022 Changelog since 4.60.0 released to PyPI on 8-May-2022
@ -253,6 +253,8 @@ _change_log = """
4.60.3.98 4.60.3.98
Better alignment of text in test harness Better alignment of text in test harness
Fixed mispelling in SystemTray.show_message - crashed if an int was passed in as the time value Fixed mispelling in SystemTray.show_message - crashed if an int was passed in as the time value
4.60.3.99
popup_get_text - Addition of history feature to bring up to same level as other popup_get_ functions.
""" """
__version__ = version.split()[0] # For PEP 396 and PEP 345 __version__ = version.split()[0] # For PEP 396 and PEP 345
@ -21327,7 +21329,7 @@ def popup_get_file(message, title=None, default_path='', default_extension='', s
def popup_get_text(message, title=None, default_text='', password_char='', size=(None, None), button_color=None, def popup_get_text(message, title=None, default_text='', password_char='', size=(None, None), button_color=None,
background_color=None, text_color=None, icon=None, font=None, no_titlebar=False, background_color=None, text_color=None, icon=None, font=None, no_titlebar=False,
grab_anywhere=False, keep_on_top=None, location=(None, None), relative_location=(None, None), image=None, modal=True): grab_anywhere=False, keep_on_top=None, location=(None, None), relative_location=(None, None), image=None, history=False, history_setting_filename=None, modal=True):
""" """
Display Popup with text entry field. Returns the text entered or None if closed / cancelled Display Popup with text entry field. Returns the text entered or None if closed / cancelled
@ -21337,7 +21339,7 @@ def popup_get_text(message, title=None, default_text='', password_char='', size=
:type title: (str) :type title: (str)
:param default_text: default value to put into input area :param default_text: default value to put into input area
:type default_text: (str) :type default_text: (str)
:param password_char: character to be shown instead of actually typed characters :param password_char: character to be shown instead of actually typed characters. WARNING - if history=True then can't hide passwords
:type password_char: (str) :type password_char: (str)
:param size: (width, height) of the InputText Element :param size: (width, height) of the InputText Element
:type size: (int, int) :type size: (int, int)
@ -21363,12 +21365,32 @@ def popup_get_text(message, title=None, default_text='', password_char='', size=
:type relative_location: (int, int) :type relative_location: (int, int)
:param image: Image to include at the top of the popup window :param image: Image to include at the top of the popup window
:type image: (str) or (bytes) :type image: (str) or (bytes)
:param history: If True then enable a "history" feature that will display previous entries used. Uses settings filename provided or default if none provided
:type history: bool
:param history_setting_filename: Filename to use for the User Settings. Will store list of previous entries in this settings file
:type history_setting_filename: (str)
:param modal: If True then makes the popup will behave like a Modal window... all other windows are non-operational until this one is closed. Default = True :param modal: If True then makes the popup will behave like a Modal window... all other windows are non-operational until this one is closed. Default = True
:type modal: bool :type modal: bool
:return: Text entered or None if window was closed or cancel button clicked :return: Text entered or None if window was closed or cancel button clicked
:rtype: str | None :rtype: str | None
""" """
# First setup the history settings file if history feature is enabled
if history and history_setting_filename is not None:
try:
history_settings = UserSettings(history_setting_filename)
except Exception as e:
_error_popup_with_traceback('popup_get_file - Something is wrong with your supplied history settings filename',
'Exception: {}'.format(e))
return None
elif history:
history_settings_filename = os.path.basename(inspect.stack()[1].filename)
history_settings_filename = os.path.splitext(history_settings_filename)[0] + '.json'
history_settings = UserSettings(history_settings_filename)
else:
history_settings = None
if image is not None: if image is not None:
if isinstance(image, str): if isinstance(image, str):
layout = [[Image(filename=image)]] layout = [[Image(filename=image)]]
@ -21377,21 +21399,46 @@ def popup_get_text(message, title=None, default_text='', password_char='', size=
else: else:
layout = [[]] layout = [[]]
layout += [[Text(message, auto_size_text=True, text_color=text_color, background_color=background_color)], layout += [[Text(message, auto_size_text=True, text_color=text_color, background_color=background_color)]]
[InputText(default_text=default_text, size=size, key='-INPUT-', password_char=password_char)], if not history:
[Button('Ok', size=(6, 1), bind_return_key=True), Button('Cancel', size=(6, 1))]] layout += [[InputText(default_text=default_text, size=size, key='-INPUT-', password_char=password_char)]]
else:
text_list = history_settings.get("-PSG text list-", [])
last_entry = text_list[0] if text_list else default_text
layout += [[Combo(text_list, default_value=last_entry, key='-INPUT-', size=size if size != (None, None) else (80, 1), bind_return_key=True),
Button('Clear History', tooltip='Clears the list of files shown in the combobox')]]
layout += [[Button('Ok', size=(6, 1), bind_return_key=True), Button('Cancel', size=(6, 1))]]
window = Window(title=title or message, layout=layout, icon=icon, auto_size_text=True, button_color=button_color, no_titlebar=no_titlebar, window = Window(title=title or message, layout=layout, icon=icon, auto_size_text=True, button_color=button_color, no_titlebar=no_titlebar,
background_color=background_color, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location, relative_location=relative_location, finalize=True, modal=modal, font=font) background_color=background_color, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location, relative_location=relative_location, finalize=True, modal=modal, font=font)
button, values = window.read()
while True:
event, values = window.read()
if event in ('Cancel', WIN_CLOSED):
break
elif event == 'Clear History':
history_settings.set('-PSG text list-', [])
window['-INPUT-'].update('', [])
popup_quick_message('History of Previous Choices Cleared', background_color='red', text_color='white', font='_ 20', keep_on_top=True)
elif event in ('Ok', '-INPUT-'):
if values['-INPUT-'] != '':
if history_settings is not None:
list_of_entries = history_settings.get('-PSG text list-', [])
if values['-INPUT-'] in list_of_entries:
list_of_entries.remove(values['-INPUT-'])
list_of_entries.insert(0, values['-INPUT-'])
history_settings.set('-PSG text list-', list_of_entries)
break
window.close() window.close()
del window del window
if button != 'Ok': if event in ('Cancel', WIN_CLOSED):
return None return None
else: else:
path = values['-INPUT-'] text = values['-INPUT-']
return path return text
def popup_get_date(start_mon=None, start_day=None, start_year=None, begin_at_sunday_plus=0, no_titlebar=True, title='Choose Date', keep_on_top=True, def popup_get_date(start_mon=None, start_day=None, start_year=None, begin_at_sunday_plus=0, no_titlebar=True, title='Choose Date', keep_on_top=True,