From 51772d34201a6369cd227da8f5e6682285df03fb Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy <mike_barnett@hotmail.com> Date: Fri, 7 Dec 2018 09:34:38 -0500 Subject: [PATCH] Recipe for launching a program --- docs/cookbook.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/docs/cookbook.md b/docs/cookbook.md index b8478913..f10d448b 100644 --- a/docs/cookbook.md +++ b/docs/cookbook.md @@ -548,6 +548,35 @@ This Window doesn't close after button clicks. To achieve this the buttons are ExecuteCommandSubprocess(value[0]) ``` + +## Launch a Program With a Button + +Very simple script that will launch a program as a subprocess. Great for making a desktop launcher toolbar. + +```python +import subprocess +import PySimpleGUI as sg + +CHROME = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" + + +layout = [ [sg.Text('Text area', key='_TEXT_')], + [sg.Input(do_not_clear=True, key='_URL_')], + [sg.Button('Chrome'), sg.Button('Exit')]] + +window = sg.Window('Window Title').Layout(layout) + +while True: # Event Loop + event, values = window.Read() + print(event, values) + if event is None or event == 'Exit': + break + if event == 'Chrome': + sp = subprocess.Popen([CHROME, values['_URL_']], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + +window.Close() +``` + ## Machine Learning GUI A standard non-blocking GUI with lots of inputs. @@ -718,7 +747,9 @@ This simple program keep a window open, taking input values until the user termi break ``` - ## One Element Updating Another +## One Element Updating Another - Compound Elements + + You can easily build "compound elements" in a single like of code. This recipe shows you how to add a numeric value onto a slider.