2018-09-27 20:24:09 +00:00
#!/usr/bin/env python
2018-09-24 22:01:00 +00:00
import sys
2018-09-28 18:57:37 +00:00
if sys . version_info [ 0 ] > = 3 :
2018-09-27 20:24:09 +00:00
import PySimpleGUI as sg
2018-09-28 18:57:37 +00:00
else :
import PySimpleGUI27 as sg
2018-09-11 03:24:20 +00:00
'''
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 ( ) :
2018-09-24 22:01:00 +00:00
# ------- Make a new Window ------- #
2018-09-11 03:24:20 +00:00
sg . ChangeLookAndFeel ( ' GreenTan ' ) # give our form a spiffy set of colors
2018-09-24 22:01:00 +00:00
layout = [ [ sg . Text ( ' Your output will go here ' , size = ( 40 , 1 ) ) ] ,
2018-09-11 03:24:20 +00:00
[ 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 ) ,
2018-09-24 22:01:00 +00:00
sg . ReadButton ( ' SEND ' , button_color = ( sg . YELLOWS [ 0 ] , sg . BLUES [ 0 ] ) , bind_return_key = True ) ,
sg . Button ( ' EXIT ' , button_color = ( sg . YELLOWS [ 0 ] , sg . GREENS [ 0 ] ) ) ] ]
window = sg . Window ( ' Chat window with history ' , default_element_size = ( 30 , 2 ) , font = ( ' Helvetica ' , ' 13 ' ) , default_button_element_size = ( 8 , 2 ) , return_keyboard_events = True ) . Layout ( layout )
2018-09-11 03:24:20 +00:00
# ---===--- Loop taking in user input and using it --- #
command_history = [ ]
history_offset = 0
while True :
2018-10-15 20:07:23 +00:00
( event , value ) = window . Read ( )
if event is ' SEND ' :
2018-09-11 03:24:20 +00:00
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
2018-09-24 22:01:00 +00:00
window . FindElement ( ' query ' ) . Update ( ' ' ) # manually clear input because keyboard events blocks clear
window . FindElement ( ' history ' ) . Update ( ' \n ' . join ( command_history [ - 3 : ] ) )
2018-10-15 20:07:23 +00:00
elif event is None or event is ' EXIT ' : # quit if exit event or X
2018-09-11 03:24:20 +00:00
break
2018-10-15 20:07:23 +00:00
elif ' Up ' in event and len ( command_history ) :
2018-09-11 03:24:20 +00:00
command = command_history [ history_offset ]
history_offset - = 1 * ( history_offset > 0 ) # decrement is not zero
2018-09-24 22:01:00 +00:00
window . FindElement ( ' query ' ) . Update ( command )
2018-10-15 20:07:23 +00:00
elif ' Down ' in event and len ( command_history ) :
2018-09-11 03:24:20 +00:00
history_offset + = 1 * ( history_offset < len ( command_history ) - 1 ) # increment up to end of list
command = command_history [ history_offset ]
2018-09-24 22:01:00 +00:00
window . FindElement ( ' query ' ) . Update ( command )
2018-10-15 20:07:23 +00:00
elif ' Escape ' in event :
2018-09-24 22:01:00 +00:00
window . FindElement ( ' query ' ) . Update ( ' ' )
2018-09-11 03:24:20 +00:00
2018-09-24 22:01:00 +00:00
sys . exit ( 69 )
2018-09-11 03:24:20 +00:00
ChatBotWithHistory ( )