From b11eeae7efa7bf308a076b21ababe0505be5e58b Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Tue, 28 Aug 2018 19:02:02 -0400 Subject: [PATCH 1/3] FlexForm.Refresh method, Open() - new button shortcut function --- PySimpleGUI.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) 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): From 3c5b1d25524f3c0678b06768b6ac58bd2cab09e3 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Tue, 28 Aug 2018 19:46:27 -0400 Subject: [PATCH 2/3] Quickly add GUI to script Recipe --- Demo_Script_Parameters.py | 25 +++++++++++++++++++++++++ docs/cookbook.md | 22 ++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 Demo_Script_Parameters.py 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 From f5d86d2fa227784b891a47318831a817ceec6a9e Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Tue, 28 Aug 2018 19:48:16 -0400 Subject: [PATCH 3/3] Formatting --- docs/cookbook.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/cookbook.md b/docs/cookbook.md index 976ac6dd..44453e27 100644 --- a/docs/cookbook.md +++ b/docs/cookbook.md @@ -69,6 +69,9 @@ Browse for a filename that is populated into the input field. ## 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 @@ -84,7 +87,7 @@ Quickly add a GUI allowing the user to browse for a filename if a filename is no raise SystemExit("Cancelling: no filename supplied") -![script front-end](https://user-images.githubusercontent.com/13696193/44756573-39e9c380-aaf9-11e8-97b4-6679f9f5bd46.jpg) + --------------