()
| 17 | |
| 18 | |
| 19 | def ChatBotWithHistory(): |
| 20 | # ------- Make a new Window ------- # |
| 21 | # give our form a spiffy set of colors |
| 22 | sg.theme('GreenTan') |
| 23 | |
| 24 | layout = [[sg.Text('Your output will go here', size=(40, 1))], |
| 25 | [sg.Output(size=(127, 30), font=('Helvetica 10'))], |
| 26 | [sg.Text('Command History'), |
| 27 | sg.Text('', size=(20, 3), key='history')], |
| 28 | [sg.ML(size=(85, 5), enter_submits=True, key='query', do_not_clear=False), |
| 29 | sg.Button('SEND', button_color=(sg.YELLOWS[0], sg.BLUES[0]), bind_return_key=True), |
| 30 | sg.Button('EXIT', button_color=(sg.YELLOWS[0], sg.GREENS[0]))]] |
| 31 | |
| 32 | window = sg.Window('Chat window with history', layout, |
| 33 | default_element_size=(30, 2), |
| 34 | font=('Helvetica', ' 13'), |
| 35 | default_button_element_size=(8, 2), |
| 36 | return_keyboard_events=True) |
| 37 | |
| 38 | # ---===--- Loop taking in user input and using it --- # |
| 39 | command_history = [] |
| 40 | history_offset = 0 |
| 41 | |
| 42 | while True: |
| 43 | event, value = window.read() |
| 44 | |
| 45 | if event == 'SEND': |
| 46 | query = value['query'].rstrip() |
| 47 | # EXECUTE YOUR COMMAND HERE |
| 48 | print('The command you entered was {}'.format(query)) |
| 49 | command_history.append(query) |
| 50 | history_offset = len(command_history)-1 |
| 51 | # manually clear input because keyboard events blocks clear |
| 52 | window['query'].update('') |
| 53 | window['history'].update('\n'.join(command_history[-3:])) |
| 54 | |
| 55 | elif event in (sg.WIN_CLOSED, 'EXIT'): # quit if exit event or X |
| 56 | break |
| 57 | |
| 58 | elif 'Up' in event and len(command_history): |
| 59 | command = command_history[history_offset] |
| 60 | # decrement is not zero |
| 61 | history_offset -= 1 * (history_offset > 0) |
| 62 | window['query'].update(command) |
| 63 | |
| 64 | elif 'Down' in event and len(command_history): |
| 65 | # increment up to end of list |
| 66 | history_offset += 1 * (history_offset < len(command_history)-1) |
| 67 | command = command_history[history_offset] |
| 68 | window['query'].update(command) |
| 69 | |
| 70 | elif 'Escape' in event: |
| 71 | window['query'].update('') |
| 72 | |
| 73 | |
| 74 | ChatBotWithHistory() |
no test coverage detected