diff --git a/Demo_Script_Parameters.py b/Demo_Script_Parameters.py new file mode 100644 index 00000000..aa16c065 --- /dev/null +++ b/Demo_Script_Parameters.py @@ -0,0 +1,25 @@ +import PySimpleGUI as sg +import sys + +''' +Quickly add a GUI to your script! + +This simple script shows a 1-line-GUI addition to a typical Python command line script. + +Previously this script accepted 1 parameter on the command line. When executed, that +parameter is read into the variable fname. + +The 1-line-GUI shows a form that allows the user to browse to find the filename. The GUI +stores the result in the variable fname, just like the command line parsing did. +''' + +if len(sys.argv) == 1: + button, (fname,) = sg.FlexForm('My Script').LayoutAndRead([[sg.T('Document to open')], + [sg.In(), sg.FileBrowse()], + [sg.Open(), sg.Cancel()]]) +else: + fname = sys.argv[1] + +if not fname: + sg.MsgBox("Cancel", "No filename supplied") + raise SystemExit("Cancelling: no filename supplied") diff --git a/docs/cookbook.md b/docs/cookbook.md index 871ca197..976ac6dd 100644 --- a/docs/cookbook.md +++ b/docs/cookbook.md @@ -66,6 +66,28 @@ Browse for a filename that is populated into the input field. print(button, source_filename) -------------------------- +## Add GUI to Front-End of Script +Quickly add a GUI allowing the user to browse for a filename if a filename is not supplied on the command line using this 1-line GUI. It's the best of both worlds. + + import PySimpleGUI as sg + import sys + + if len(sys.argv) == 1: + button, (fname,) = sg.FlexForm('My Script').LayoutAndRead([[sg.T('Document to open')], + [sg.In(), sg.FileBrowse()], + [sg.Open(), sg.Cancel()]]) + else: + fname = sys.argv[1] + + if not fname: + sg.MsgBox("Cancel", "No filename supplied") + raise SystemExit("Cancelling: no filename supplied") + + +![script front-end](https://user-images.githubusercontent.com/13696193/44756573-39e9c380-aaf9-11e8-97b4-6679f9f5bd46.jpg) + +-------------- + ## Compare 2 Files Browse to get 2 file names that can be then compared. Uses a context manager