From 7a7513e00d9184a0a769dce32500f06899098523 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Wed, 22 Jan 2020 16:21:46 -0500 Subject: [PATCH] Fixed wasn't returning window when creating popup. Made a main func instead of a flat program structure --- DemoPrograms/Demo_Input_Auto_Complete.py | 132 +++++++++++------------ 1 file changed, 65 insertions(+), 67 deletions(-) diff --git a/DemoPrograms/Demo_Input_Auto_Complete.py b/DemoPrograms/Demo_Input_Auto_Complete.py index 9f392e01..22a87362 100644 --- a/DemoPrograms/Demo_Input_Auto_Complete.py +++ b/DemoPrograms/Demo_Input_Auto_Complete.py @@ -2,20 +2,17 @@ import PySimpleGUI as sg import re ''' - Exampel of Input element features. + Demo of using a borderless window to show possible matches for autocomplete feature ''' def autocomplete_popup_show(text_list): - autocomplete_popup_layout = [ - [sg.Listbox(values=text_list, - size=(15, len(text_list)), - change_submits=True, bind_return_key=True, - key='-FLOATING-LISTBOX-', enable_events=True) - ] - ] + layout = [[ sg.Listbox(values=text_list, + size=(15, len(text_list)), + change_submits=True, bind_return_key=True, + key='-FLOATING-LISTBOX-', enable_events=True) ]] - autocomplete_popup = sg.Window("Borderless Window", - autocomplete_popup_layout, + return sg.Window("Borderless Window", + layout, default_element_size=(12, 1), auto_size_text=False, keep_on_top=True, no_titlebar=True, grab_anywhere=True, @@ -25,76 +22,77 @@ def autocomplete_popup_show(text_list): default_button_element_size=(12, 1), location=(1320, 622), finalize=True) - return window - def predict_text(input, lista): pattern = re.compile('.*' + input + '.*') return [w for w in lista if re.match(pattern, w)] -choices = ['ABC' + str(i) for i in range(30)] # dummy data +def main(): -layout = [[sg.Text('Your typed chars appear here:')], - [sg.Input(key='-INPUT-', size=(10, 1))], - [sg.Button('Show'), sg.Button('Exit')], ] + choices = ['ABC' + str(i) for i in range(30)] # dummy data -window = sg.Window('Window Title', layout, return_keyboard_events=True) + layout = [[sg.Text('Your typed chars appear here:')], + [sg.Input(key='-INPUT-', size=(10, 1))], + [sg.Button('Show'), sg.Button('Exit')], ] -sel_item = -1 -skip_event = False -while True: # Event Loop - event, values = window.read(timeout=500) + window = sg.Window('Autocomplete Demo', layout, return_keyboard_events=True) - if event in (None, 'Exit'): - break + sel_item = -1 + fwindow = list_elem = values2 = None + while True: # Event Loop + event, values = window.read(timeout=500) - if event != sg.TIMEOUT_KEY: - # print(f'event1 {event}') - in_val = values['-INPUT-'] - prediction_list = predict_text(str(in_val), choices) - if prediction_list: - try: + if event in (None, 'Exit'): + break + + if event != sg.TIMEOUT_KEY: + # print(f'event1 {event}') + in_val = values['-INPUT-'] + prediction_list = predict_text(str(in_val), choices) + if prediction_list: + try: + fwindow.close() + except: + pass + fwindow = autocomplete_popup_show(prediction_list) + list_elem = fwindow['-FLOATING-LISTBOX-'] + if event == '_COMBO_': + sg.popup('Chose', values['_COMBO_']) + + if event.startswith('Down') or event.startswith('special 16777237'): + sel_item = sel_item + (sel_item < len(prediction_list)) + list_elem.update(set_to_index=sel_item) + + elif event.startswith('Up') or event.startswith('special 16777235'): + sel_item = sel_item - (sel_item > 0) + list_elem.update(set_to_index=sel_item) + + if event == '\r' or event.startswith('special 16777220'): + chosen = values2['-FLOATING-LISTBOX-'] if values2 is not None else None + if chosen: + window['-INPUT-'].update(chosen[0], select=True) fwindow.close() - except: - pass - fwindow = autocomplete_popup_show(prediction_list) - list_elem = fwindow['-FLOATING-LISTBOX-'] - if event == '_COMBO_': - sg.popup('Chose', values['_COMBO_']) + sel_item = -1 - if event.startswith('Down') or event.startswith('special 16777237'): - sel_item = sel_item + (sel_item < len(prediction_list)) - list_elem.update(set_to_index=sel_item) - skip_event = True + if event.startswith('Escape') or event.startswith('special 16777216'): + window['-INPUT-'].update('') - elif event.startswith('Up') or event.startswith('special 16777235'): - sel_item = sel_item - (sel_item > 0) - list_elem.update(set_to_index=sel_item) - skip_event = True + try: + event2, values2 = fwindow.read(timeout=10) + # if event2 == '-FLOATING-LISTBOX-' and skip_event and QT: + # skip_event = False + if event2 != sg.TIMEOUT_KEY and event2 is not None: + # print(f'event2 {event2}') + fwindow.close() + window['-INPUT-'].update(values2['-FLOATING-LISTBOX-'] + [0], select=True) + sel_item = -1 + fwindow = None + except: + pass - if event == '\r' or event.startswith('special 16777220'): - chosen = values2['-FLOATING-LISTBOX-'] - window['-INPUT-'].update(values2['-FLOATING-LISTBOX-'] - [0], select=True) - fwindow.close() - sel_item = -1 + window.close() - if event.startswith('Escape') or event.startswith('special 16777216'): - window['-INPUT-'].update('') - - try: - event2, values2 = fwindow.read(timeout=10) - # if event2 == '-FLOATING-LISTBOX-' and skip_event and QT: - # skip_event = False - if event2 != sg.TIMEOUT_KEY and event2 is not None: - # print(f'event2 {event2}') - fwindow.close() - window['-INPUT-'].update(values2['-FLOATING-LISTBOX-'] - [0], select=True) - sel_item = -1 - fwindow = None - except: - pass - -window.close() +if __name__ == '__main__': + main()