Make and show a window (PySimpleGUI form) that takes user input and sends to the HowDoI web oracle Excellent example of 2 GUI concepts 1. Output Element that will show text in a scrolled window 2. Non-Window-Closing Buttons - These buttons will cause the form to return with
()
| 16 | |
| 17 | |
| 18 | def HowDoI(): |
| 19 | ''' |
| 20 | Make and show a window (PySimpleGUI form) that takes user input and sends to the HowDoI web oracle |
| 21 | Excellent example of 2 GUI concepts |
| 22 | 1. Output Element that will show text in a scrolled window |
| 23 | 2. Non-Window-Closing Buttons - These buttons will cause the form to return with the form's values, but doesn't close the form |
| 24 | :return: never returns |
| 25 | ''' |
| 26 | # ------- Make a new Window ------- # |
| 27 | # give our form a spiffy set of colors |
| 28 | sg.theme('GreenTan') |
| 29 | |
| 30 | layout = [ |
| 31 | [sg.Text('Ask and your answer will appear here....', size=(40, 1))], |
| 32 | [sg.Output(size=(120, 30), font=('Helvetica 10'))], |
| 33 | [sg.Spin(values=(1, 2, 3, 4), initial_value=1, size=(2, 1), key='Num Answers', font='Helvetica 15'), |
| 34 | sg.Text('Num Answers', font='Helvetica 15'), sg.CBox( |
| 35 | 'Display Full Text', key='full text', font='Helvetica 15'), |
| 36 | sg.Text('Command History', font='Helvetica 15'), sg.Text('', size=(40, 3), text_color=sg.BLUES[0], key='history')], |
| 37 | [sg.MLine(size=(85, 5), enter_submits=True, key='query', do_not_clear=False), |
| 38 | sg.Button('SEND', button_color=( |
| 39 | sg.YELLOWS[0], sg.BLUES[0]), bind_return_key=True), |
| 40 | sg.Button('EXIT', button_color=(sg.YELLOWS[0], sg.GREENS[0]))] |
| 41 | ] |
| 42 | |
| 43 | window = sg.Window('How Do I ??', layout, default_element_size=(30, 2), |
| 44 | font=('Helvetica', ' 13'), |
| 45 | default_button_element_size=(8, 2), |
| 46 | return_keyboard_events=True, no_titlebar=True, |
| 47 | grab_anywhere=True) |
| 48 | # ---===--- Loop taking in user input and using it to query HowDoI --- # |
| 49 | command_history = [] |
| 50 | history_offset = 0 |
| 51 | while True: |
| 52 | event, values = window.read() |
| 53 | if event == 'SEND': |
| 54 | query = values['query'].rstrip() |
| 55 | # print(query) |
| 56 | # send the string to HowDoI |
| 57 | QueryHowDoI(query, values['Num Answers'], values['full text']) |
| 58 | command_history.append(query) |
| 59 | history_offset = len(command_history)-1 |
| 60 | # manually clear input because keyboard events blocks clear |
| 61 | window['query'].update('') |
| 62 | window['history'].update('\n'.join(command_history[-3:])) |
| 63 | elif event == None or event == 'EXIT': # if exit button or closed using X |
| 64 | break |
| 65 | # scroll back in history |
| 66 | elif 'Up' in event and len(command_history): |
| 67 | command = command_history[history_offset] |
| 68 | # decrement is not zero |
| 69 | history_offset -= 1 * (history_offset > 0) |
| 70 | window['query'].update(command) |
| 71 | # scroll forward in history |
| 72 | elif 'Down' in event and len(command_history): |
| 73 | # increment up to end of list |
| 74 | history_offset += 1 * (history_offset < len(command_history)-1) |
| 75 | command = command_history[history_offset] |
no test coverage detected