PySimpleGUI/Demo_Script_Launcher.py

73 lines
2.6 KiB
Python
Raw Normal View History

import PySimpleGUI as sg
2018-08-30 00:50:10 +00:00
import glob
import ntpath
import subprocess
2018-09-08 03:23:45 +00:00
LOCATION_OF_YOUR_SCRIPTS = ''
2018-08-30 00:50:10 +00:00
# Execute the command. Will not see the output from the command until it completes.
def execute_command_blocking(command, *args):
2018-09-08 03:23:45 +00:00
expanded_args = []
for a in args:
expanded_args.append(a)
# expanded_args += a
try:
2018-09-08 03:23:45 +00:00
sp = subprocess.Popen([command,expanded_args], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = sp.communicate()
if out:
print(out.decode("utf-8"))
if err:
print(err.decode("utf-8"))
except:
out = ''
return out
2018-08-30 00:50:10 +00:00
# Executes command and immediately returns. Will not see anything the script outputs
def execute_command_nonblocking(command, *args):
2018-09-08 03:23:45 +00:00
expanded_args = []
for a in args:
expanded_args += a
2018-08-30 00:50:10 +00:00
try:
2018-09-08 03:23:45 +00:00
sp = subprocess.Popen([command,expanded_args], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2018-08-30 00:50:10 +00:00
except: pass
def Launcher2():
sg.ChangeLookAndFeel('GreenTan')
window = sg.Window('Script launcher')
2018-08-30 00:50:10 +00:00
filelist = glob.glob(LOCATION_OF_YOUR_SCRIPTS+'*.py')
namesonly = []
for file in filelist:
namesonly.append(ntpath.basename(file))
layout = [
[sg.Listbox(values=namesonly, size=(30, 19), select_mode=sg.SELECT_MODE_EXTENDED, key='demolist'), sg.Output(size=(88, 20), font='Courier 10')],
2018-08-30 01:00:08 +00:00
[sg.Checkbox('Wait for program to complete', default=False, key='wait')],
[sg.ReadButton('Run'), sg.ReadButton('Shortcut 1'), sg.ReadButton('Fav Program'), sg.Button('EXIT')],
2018-08-30 00:50:10 +00:00
]
window.Layout(layout)
2018-08-30 00:50:10 +00:00
# ---===--- Loop taking in user input and using it to query HowDoI --- #
while True:
(button, value) = window.Read()
2018-08-30 00:50:10 +00:00
if button in ('EXIT', None):
break # exit button clicked
if button in ('Shortcut 1', 'Fav Program'):
print('Quickly launch your favorite programs using these shortcuts')
print('Or copy files to your github folder. Or anything else you type on the command line')
# copyfile(source, dest)
elif button is 'Run':
for index, file in enumerate(value['demolist']):
print('Launching %s'%file)
window.Refresh() # make the print appear immediately
2018-08-30 01:00:08 +00:00
if value['wait']:
execute_command_blocking(LOCATION_OF_YOUR_SCRIPTS + file)
else:
execute_command_nonblocking(LOCATION_OF_YOUR_SCRIPTS + file)
2018-08-30 00:50:10 +00:00
if __name__ == '__main__':
2018-08-30 00:50:10 +00:00
Launcher2()