2018-09-27 20:24:09 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import sys
|
2019-10-23 20:10:03 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-09-10 22:42:05 +00:00
|
|
|
import subprocess
|
|
|
|
import os
|
2018-09-27 20:24:09 +00:00
|
|
|
|
2018-09-10 22:42:05 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
Demo_Toolbar - A floating toolbar with quick launcher
|
|
|
|
|
|
|
|
One cool PySimpleGUI demo. Shows borderless windows, grab_anywhere, tight button layout
|
|
|
|
You can setup a specific program to launch when a button is clicked, or use the
|
|
|
|
Combobox to select a .py file found in the root folder, and run that file.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
ROOT_PATH = './'
|
|
|
|
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
def Launcher():
|
2018-09-10 22:42:05 +00:00
|
|
|
|
2019-12-24 23:52:47 +00:00
|
|
|
sg.theme('Dark')
|
2018-09-10 22:42:05 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
namesonly = [f for f in os.listdir(ROOT_PATH) if f.endswith('.py')]
|
2018-09-10 22:42:05 +00:00
|
|
|
|
2018-10-29 00:01:03 +00:00
|
|
|
if len(namesonly) == 0:
|
|
|
|
namesonly = ['test 1', 'test 2', 'test 3']
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
sg.set_options(element_padding=(0, 0),
|
|
|
|
button_element_size=(12, 1), auto_size_buttons=False)
|
2018-09-10 22:42:05 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
layout = [[sg.Combo(values=namesonly, size=(35, 30), key='demofile'),
|
|
|
|
sg.Button('Run', button_color=('white', '#00168B')),
|
|
|
|
sg.Button('Program 1'),
|
|
|
|
sg.Button('Program 2'),
|
|
|
|
sg.Button('Program 3', button_color=('white', '#35008B')),
|
|
|
|
sg.Button('EXIT', button_color=('white', 'firebrick3'))],
|
|
|
|
[sg.Text('', text_color='white', size=(50, 1), key='output')]]
|
2018-09-10 22:42:05 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window = sg.Window('Floating Toolbar',
|
|
|
|
layout,
|
|
|
|
no_titlebar=True,
|
|
|
|
grab_anywhere=True,
|
|
|
|
keep_on_top=True)
|
2018-09-10 22:42:05 +00:00
|
|
|
|
2018-09-24 22:01:00 +00:00
|
|
|
# ---===--- Loop taking in user input and executing appropriate program --- #
|
2018-09-10 22:42:05 +00:00
|
|
|
while True:
|
2019-10-23 20:10:03 +00:00
|
|
|
event, values = window.read()
|
2018-10-15 20:07:23 +00:00
|
|
|
if event == 'EXIT' or event is None:
|
2018-09-10 22:42:05 +00:00
|
|
|
break # exit button clicked
|
2018-10-15 20:07:23 +00:00
|
|
|
if event == 'Program 1':
|
2018-09-10 22:42:05 +00:00
|
|
|
print('Run your program 1 here!')
|
2018-10-15 20:07:23 +00:00
|
|
|
elif event == 'Program 2':
|
2018-09-10 22:42:05 +00:00
|
|
|
print('Run your program 2 here!')
|
2018-10-15 20:07:23 +00:00
|
|
|
elif event == 'Run':
|
|
|
|
file = values['demofile']
|
2019-10-23 20:10:03 +00:00
|
|
|
print('Launching %s' % file)
|
2018-09-10 22:42:05 +00:00
|
|
|
ExecuteCommandSubprocess('python', os.path.join(ROOT_PATH, file))
|
|
|
|
else:
|
2018-10-15 20:07:23 +00:00
|
|
|
print(event)
|
2018-09-10 22:42:05 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window.close()
|
|
|
|
|
|
|
|
|
2018-09-10 22:42:05 +00:00
|
|
|
def ExecuteCommandSubprocess(command, *args, wait=False):
|
|
|
|
try:
|
|
|
|
if sys.platform == 'linux':
|
|
|
|
arg_string = ''
|
2018-09-13 23:57:58 +00:00
|
|
|
arg_string = ' '.join([str(arg) for arg in args])
|
|
|
|
# for arg in args:
|
|
|
|
# arg_string += ' ' + str(arg)
|
|
|
|
print('python3 ' + arg_string)
|
2019-10-23 20:10:03 +00:00
|
|
|
sp = subprocess.Popen(['python3 ', arg_string],
|
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
2018-09-10 22:42:05 +00:00
|
|
|
else:
|
2018-09-13 23:57:58 +00:00
|
|
|
arg_string = ' '.join([str(arg) for arg in args])
|
2019-10-23 20:10:03 +00:00
|
|
|
sp = subprocess.Popen([command, arg_string],
|
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
2018-09-13 23:57:58 +00:00
|
|
|
# sp = subprocess.Popen([command, list(args)], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2018-09-10 22:42:05 +00:00
|
|
|
|
|
|
|
if wait:
|
|
|
|
out, err = sp.communicate()
|
|
|
|
if out:
|
|
|
|
print(out.decode("utf-8"))
|
|
|
|
if err:
|
|
|
|
print(err.decode("utf-8"))
|
2019-10-23 20:10:03 +00:00
|
|
|
except:
|
|
|
|
pass
|
2018-09-10 22:42:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
Launcher()
|