Delete multiline input when doing an "Update", History added to HowDoI using up/down keys

This commit is contained in:
MikeTheWatchGuy 2018-08-28 21:05:11 -04:00
parent 01e7fd65fa
commit 9b286bb050
2 changed files with 34 additions and 15 deletions

View File

@ -1,6 +1,6 @@
import PySimpleGUI as sg
import subprocess
import howdoi
# Test this command in a dos window if you are having trouble.
HOW_DO_I_COMMAND = 'python -m howdoi.howdoi -n 2'
@ -17,27 +17,46 @@ def HowDoI():
:return: never returns
'''
# ------- Make a new FlexForm ------- #
# Set system-wide options that will affect all future forms. Give our form a spiffy look and feel
sg.SetOptions(background_color='#9FB8AD', text_element_background_color='#9FB8AD', element_background_color='#9FB8AD', scrollbar_color=None, input_elements_background_color='#F7F3EC', button_color=('white', '#475841'))
form = sg.FlexForm('How Do I ??', auto_size_text=True, default_element_size=(30, 2), icon=DEFAULT_ICON)
sg.ChangeLookAndFeel('GreenTan') # give our form a spiffy set of colors
form = sg.FlexForm('How Do I ??', default_element_size=(30, 2), icon=DEFAULT_ICON, font=('Helvetica',' 13'), default_button_element_size=(8,2), return_keyboard_events=True)
multiline_elem = sg.Multiline(size=(85, 5), enter_submits=True, key='query', do_not_clear=False)
layout = [
[sg.Text('Ask and your answer will appear here....', size=(40, 1))],
[sg.Output(size=(88, 20))],
[ sg.Spin(values=(1, 2, 3, 4), initial_value=1, size=(2, 1), key='Num Answers'), sg.T('Num Answers'), sg.Checkbox('Display Full Text', key='full text')],
[sg.Multiline(size=(85, 5), enter_submits=True, key='query'),
[sg.Output(size=(127, 30), font=('Helvetica 10'))],
[ sg.Spin(values=(1, 2, 3, 4), initial_value=1, size=(2, 1), key='Num Answers', font='Helvetica 15'), sg.T('Num Answers',font='Helvetica 15'), sg.Checkbox('Display Full Text', key='full text', font='Helvetica 15')],
[multiline_elem,
sg.ReadFormButton('SEND', button_color=(sg.YELLOWS[0], sg.BLUES[0]), bind_return_key=True),
sg.SimpleButton('EXIT', button_color=(sg.YELLOWS[0], sg.GREENS[0]))]
]
form.Layout(layout)
# ---===--- Loop taking in user input and using it to query HowDoI --- #
command_history = []
history_offset = 0
while True:
(button, value) = form.Read()
if button == 'SEND':
QueryHowDoI(value['query'], value['Num Answers'], value['full text']) # send string without carriage return on end
else:
if button is 'SEND':
query = value['query'].rstrip()
QueryHowDoI(query, value['Num Answers'], value['full text']) # send the string to HowDoI
command_history.append(query)
history_offset = len(command_history)
multiline_elem.Update('') # manually clear input because keyboard events blocks clear
elif button is None or button is 'EXIT':
break # exit button clicked
elif 'Up' in button:
history_offset -= 1 * (history_offset > 0) # decrement is not zero
command = command_history[history_offset]
multiline_elem.Update(command)
elif 'Down' in button:
history_offset += 1 * (history_offset < len(command_history)-1) # increment up to end of list
if history_offset >= len(command_history):
history_offset = len(command_history)-1
command = command_history[history_offset]
multiline_elem.Update(command)
elif 'Escape' in button:
multiline_elem.Update('') # manually clear input because keyboard events blocks clear
exit(69)
def QueryHowDoI(Query, num_answers, full_text):
@ -51,8 +70,8 @@ def QueryHowDoI(Query, num_answers, full_text):
full_text_option = ' -a' if full_text else ''
t = subprocess.Popen(howdoi_command + ' '+ Query + ' -n ' + str(num_answers)+full_text_option, stdout=subprocess.PIPE)
(output, err) = t.communicate()
print('You asked: '+ Query)
print('_______________________________________')
print('{:^88}'.format(Query.rstrip()))
print('_'*60)
print(output.decode("utf-8") )
exit_code = t.wait()

View File

@ -6,7 +6,6 @@ import tkinter.scrolledtext as tkst
import tkinter.font
import datetime
import sys
from sys import platform
import textwrap
# ----====----====----==== Constants the user CAN safely change ====----====----====----#
@ -471,6 +470,7 @@ class Multiline(Element):
return
def Update(self, NewValue):
self.TKText.delete('1.0', tk.END)
self.TKText.insert(1.0, NewValue)
def Get(self):