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/PySimpleGUI.py b/PySimpleGUI.py index 851ff285..4438b7db 100644 --- a/PySimpleGUI.py +++ b/PySimpleGUI.py @@ -1116,7 +1116,6 @@ class FlexForm: else: return self.ReturnValues - def ReadNonBlocking(self, Message=''): if self.TKrootDestroyed: return None, None @@ -1132,6 +1131,15 @@ class FlexForm: # return None, None return BuildResults(self, False, self) + def Refresh(self): + if self.TKrootDestroyed: + return + try: + rc = self.TKroot.update() + except: + pass + + def GetScreenDimensions(self): if self.TKrootDestroyed: return None, None @@ -1302,6 +1310,10 @@ def Save(button_text='Save', scale=(None, None), size=(None, None), auto_size_bu def Submit(button_text='Submit', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, bind_return_key=True,font=None, focus=False, pad=None): return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color,font=font, bind_return_key=bind_return_key, focus=focus, pad=pad) +# ------------------------- OPEN BUTTON Element lazy function ------------------------- # +def Open(button_text='Open', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, bind_return_key=True,font=None, focus=False, pad=None): + return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color,font=font, bind_return_key=bind_return_key, focus=focus, pad=pad) + # ------------------------- OK BUTTON Element lazy function ------------------------- # def OK(button_text='OK', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, bind_return_key=True, font=None,focus=False, pad=None): return Button(BUTTON_TYPE_CLOSES_WIN, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color,font=font, bind_return_key=bind_return_key, focus=focus, pad=pad) @@ -1336,7 +1348,7 @@ def SimpleButton(button_text, image_filename=None, image_size=(None, None), imag return Button(BUTTON_TYPE_CLOSES_WIN, image_filename=image_filename, image_size=image_size, image_subsample=image_subsample, button_text=button_text, border_width=border_width, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad) # ------------------------- GENERIC BUTTON Element lazy function ------------------------- # # this is the only button that REQUIRES button text field -def ReadFormButton(button_text, image_filename=None, image_size=(None, None),image_subsample=None,border_width=None,scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, bind_listbox_select=False, focus=False, pad=None): +def ReadFormButton(button_text, image_filename=None, image_size=(None, None),image_subsample=None,border_width=None,scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None): return Button(BUTTON_TYPE_READ_FORM, image_filename=image_filename, image_size=image_size, image_subsample=image_subsample, border_width=border_width, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad) def RealtimeButton(button_text, image_filename=None, image_size=(None, None),image_subsample=None,border_width=None,scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None): diff --git a/docs/cookbook.md b/docs/cookbook.md index 871ca197..44453e27 100644 --- a/docs/cookbook.md +++ b/docs/cookbook.md @@ -66,6 +66,31 @@ 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. + +![script front-end](https://user-images.githubusercontent.com/13696193/44756573-39e9c380-aaf9-11e8-97b4-6679f9f5bd46.jpg) + + + 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") + + + + +-------------- + ## Compare 2 Files Browse to get 2 file names that can be then compared. Uses a context manager