2018-08-09 17:18:04 +00:00
import PySimpleGUI as sg
2018-07-25 10:40:14 +00:00
import subprocess
2018-08-29 01:05:11 +00:00
2018-07-25 10:40:14 +00:00
# Test this command in a dos window if you are having trouble.
2018-08-29 01:08:25 +00:00
HOW_DO_I_COMMAND = ' python -m howdoi.howdoi '
2018-07-25 10:40:14 +00:00
# if you want an icon on your taskbar for this gui, then change this line of code to point to the ICO file
DEFAULT_ICON = ' E: \\ TheRealMyDocs \\ Icons \\ QuestionMark.ico '
def HowDoI ( ) :
'''
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 the form ' s values, but doesn ' t close the form
: return : never returns
'''
# ------- Make a new FlexForm ------- #
2018-08-29 01:05:11 +00:00
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 )
2018-08-29 17:36:03 +00:00
history_elem = sg . T ( ' ' , size = ( 40 , 3 ) )
2018-08-09 17:18:04 +00:00
layout = [
[ sg . Text ( ' Ask and your answer will appear here.... ' , size = ( 40 , 1 ) ) ] ,
2018-08-29 01:05:11 +00:00
[ 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 ' ) ] ,
2018-08-29 17:36:03 +00:00
[ sg . T ( ' Command History ' ) , history_elem ] ,
2018-08-29 01:05:11 +00:00
[ multiline_elem ,
2018-08-09 17:18:04 +00:00
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 )
2018-07-25 10:40:14 +00:00
# ---===--- Loop taking in user input and using it to query HowDoI --- #
2018-08-29 01:05:11 +00:00
command_history = [ ]
history_offset = 0
2018-07-25 10:40:14 +00:00
while True :
( button , value ) = form . Read ( )
2018-08-29 01:05:11 +00:00
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 )
2018-08-29 17:36:03 +00:00
history_offset = len ( command_history ) - 1
2018-08-29 01:05:11 +00:00
multiline_elem . Update ( ' ' ) # manually clear input because keyboard events blocks clear
2018-08-29 17:36:03 +00:00
history_elem . Update ( ' \n ' . join ( command_history [ - 3 : ] ) )
2018-08-29 01:08:25 +00:00
elif button is None or button is ' EXIT ' : # if exit button or closed using X
break
2018-08-29 17:36:03 +00:00
elif ' Up ' in button and len ( command_history ) : # scroll back in history
2018-08-29 01:05:11 +00:00
command = command_history [ history_offset ]
2018-08-29 17:36:03 +00:00
history_offset - = 1 * ( history_offset > 0 ) # decrement is not zero
2018-08-29 01:05:11 +00:00
multiline_elem . Update ( command )
2018-08-29 17:36:03 +00:00
elif ' Down ' in button and len ( command_history ) : # scroll forward in history
2018-08-29 01:05:11 +00:00
history_offset + = 1 * ( history_offset < len ( command_history ) - 1 ) # increment up to end of list
command = command_history [ history_offset ]
multiline_elem . Update ( command )
2018-08-29 01:08:25 +00:00
elif ' Escape ' in button : # clear currently line
multiline_elem . Update ( ' ' )
2018-07-25 10:40:14 +00:00
exit ( 69 )
2018-08-09 17:18:04 +00:00
def QueryHowDoI ( Query , num_answers , full_text ) :
2018-07-25 10:40:14 +00:00
'''
Kicks off a subprocess to send the ' Query ' to HowDoI
Prints the result , which in this program will route to a gooeyGUI window
: param Query : text english question to ask the HowDoI web engine
: return : nothing
'''
howdoi_command = HOW_DO_I_COMMAND
2018-08-09 17:18:04 +00:00
full_text_option = ' -a ' if full_text else ' '
t = subprocess . Popen ( howdoi_command + ' ' + Query + ' -n ' + str ( num_answers ) + full_text_option , stdout = subprocess . PIPE )
2018-07-25 10:40:14 +00:00
( output , err ) = t . communicate ( )
2018-08-29 01:05:11 +00:00
print ( ' {:^88} ' . format ( Query . rstrip ( ) ) )
print ( ' _ ' * 60 )
2018-07-25 10:40:14 +00:00
print ( output . decode ( " utf-8 " ) )
exit_code = t . wait ( )
if __name__ == ' __main__ ' :
HowDoI ( )