Merge pull request #195 from MikeTheWatchGuy/Dev-latest
New Demo - Chat without history... a simplified chat
This commit is contained in:
commit
fd34cdc0d9
33
Demo_Chat.py
33
Demo_Chat.py
|
@ -1,58 +1,31 @@
|
||||||
import PySimpleGUI as sg
|
import PySimpleGUI as sg
|
||||||
|
|
||||||
'''
|
'''
|
||||||
A chatbot with history
|
A chat window. Add call to your send-routine, print the response and you're done
|
||||||
Scroll up and down through prior commands using the arrow keys
|
|
||||||
Special keyboard keys:
|
|
||||||
Up arrow - scroll up in commands
|
|
||||||
Down arrow - scroll down in commands
|
|
||||||
Escape - clear current command
|
|
||||||
Control C - exit form
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def ChatBotWithHistory():
|
|
||||||
# ------- Make a new FlexForm ------- #
|
# ------- Make a new FlexForm ------- #
|
||||||
sg.ChangeLookAndFeel('GreenTan') # give our form a spiffy set of colors
|
sg.ChangeLookAndFeel('GreenTan') # give our form a spiffy set of colors
|
||||||
|
|
||||||
form = sg.FlexForm('Chat window with history', default_element_size=(30, 2), font=('Helvetica',' 13'), default_button_element_size=(8,2), return_keyboard_events=True)
|
form = sg.FlexForm('Chat window', default_element_size=(30, 2), font=('Helvetica',' 13'), default_button_element_size=(8,2))
|
||||||
|
|
||||||
layout = [
|
layout = [
|
||||||
[sg.Text('Your output will go here', size=(40, 1))],
|
[sg.Text('Your output will go here', size=(40, 1))],
|
||||||
[sg.Output(size=(127, 30), font=('Helvetica 10'))],
|
[sg.Output(size=(127, 30), font=('Helvetica 10'))],
|
||||||
[sg.T('Command History'), sg.T('', size=(20,3), key='history')],
|
[sg.Multiline(size=(85, 5), enter_submits=True, key='query'),
|
||||||
[sg.Multiline(size=(85, 5), enter_submits=True, key='query', do_not_clear=False),
|
|
||||||
sg.ReadFormButton('SEND', button_color=(sg.YELLOWS[0], sg.BLUES[0]), bind_return_key=True),
|
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]))]
|
sg.SimpleButton('EXIT', button_color=(sg.YELLOWS[0], sg.GREENS[0]))]
|
||||||
]
|
]
|
||||||
form.Layout(layout)
|
form.Layout(layout)
|
||||||
|
|
||||||
# ---===--- Loop taking in user input and using it --- #
|
# ---===--- Loop taking in user input and using it --- #
|
||||||
command_history = []
|
|
||||||
history_offset = 0
|
|
||||||
while True:
|
while True:
|
||||||
(button, value) = form.Read()
|
(button, value) = form.Read()
|
||||||
if button is 'SEND':
|
if button is 'SEND':
|
||||||
query = value['query'].rstrip()
|
query = value['query'].rstrip()
|
||||||
# EXECUTE YOUR COMMAND HERE
|
# EXECUTE YOUR COMMAND HERE
|
||||||
print('The command you entered was {}'.format(query))
|
print('The command you entered was {}'.format(query))
|
||||||
command_history.append(query)
|
|
||||||
history_offset = len(command_history)-1
|
|
||||||
form.FindElement('query').Update('') # manually clear input because keyboard events blocks clear
|
|
||||||
form.FindElement('history').Update('\n'.join(command_history[-3:]))
|
|
||||||
elif button is None or button is 'EXIT': # quit if exit button or X
|
elif button is None or button is 'EXIT': # quit if exit button or X
|
||||||
break
|
break
|
||||||
elif 'Up' in button and len(command_history):
|
|
||||||
command = command_history[history_offset]
|
|
||||||
history_offset -= 1 * (history_offset > 0) # decrement is not zero
|
|
||||||
form.FindElement('query').Update(command)
|
|
||||||
elif 'Down' in button and len(command_history):
|
|
||||||
history_offset += 1 * (history_offset < len(command_history)-1) # increment up to end of list
|
|
||||||
command = command_history[history_offset]
|
|
||||||
form.FindElement('query').Update(command)
|
|
||||||
elif 'Escape' in button:
|
|
||||||
form.FindElement('query').Update('')
|
|
||||||
|
|
||||||
exit(69)
|
exit(69)
|
||||||
|
|
||||||
|
|
||||||
ChatBotWithHistory()
|
|
||||||
|
|
|
@ -0,0 +1,58 @@
|
||||||
|
import PySimpleGUI as sg
|
||||||
|
|
||||||
|
'''
|
||||||
|
A chatbot with history
|
||||||
|
Scroll up and down through prior commands using the arrow keys
|
||||||
|
Special keyboard keys:
|
||||||
|
Up arrow - scroll up in commands
|
||||||
|
Down arrow - scroll down in commands
|
||||||
|
Escape - clear current command
|
||||||
|
Control C - exit form
|
||||||
|
'''
|
||||||
|
|
||||||
|
def ChatBotWithHistory():
|
||||||
|
# ------- Make a new FlexForm ------- #
|
||||||
|
sg.ChangeLookAndFeel('GreenTan') # give our form a spiffy set of colors
|
||||||
|
|
||||||
|
form = sg.FlexForm('Chat window with history', default_element_size=(30, 2), font=('Helvetica',' 13'), default_button_element_size=(8,2), return_keyboard_events=True)
|
||||||
|
|
||||||
|
layout = [
|
||||||
|
[sg.Text('Your output will go here', size=(40, 1))],
|
||||||
|
[sg.Output(size=(127, 30), font=('Helvetica 10'))],
|
||||||
|
[sg.T('Command History'), sg.T('', size=(20,3), key='history')],
|
||||||
|
[sg.Multiline(size=(85, 5), enter_submits=True, key='query', do_not_clear=False),
|
||||||
|
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 --- #
|
||||||
|
command_history = []
|
||||||
|
history_offset = 0
|
||||||
|
while True:
|
||||||
|
(button, value) = form.Read()
|
||||||
|
if button is 'SEND':
|
||||||
|
query = value['query'].rstrip()
|
||||||
|
# EXECUTE YOUR COMMAND HERE
|
||||||
|
print('The command you entered was {}'.format(query))
|
||||||
|
command_history.append(query)
|
||||||
|
history_offset = len(command_history)-1
|
||||||
|
form.FindElement('query').Update('') # manually clear input because keyboard events blocks clear
|
||||||
|
form.FindElement('history').Update('\n'.join(command_history[-3:]))
|
||||||
|
elif button is None or button is 'EXIT': # quit if exit button or X
|
||||||
|
break
|
||||||
|
elif 'Up' in button and len(command_history):
|
||||||
|
command = command_history[history_offset]
|
||||||
|
history_offset -= 1 * (history_offset > 0) # decrement is not zero
|
||||||
|
form.FindElement('query').Update(command)
|
||||||
|
elif 'Down' in button and len(command_history):
|
||||||
|
history_offset += 1 * (history_offset < len(command_history)-1) # increment up to end of list
|
||||||
|
command = command_history[history_offset]
|
||||||
|
form.FindElement('query').Update(command)
|
||||||
|
elif 'Escape' in button:
|
||||||
|
form.FindElement('query').Update('')
|
||||||
|
|
||||||
|
exit(69)
|
||||||
|
|
||||||
|
|
||||||
|
ChatBotWithHistory()
|
Loading…
Reference in New Issue