From 038ce5eb6a3f16fbaf50590ec8358bff899b997a Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Tue, 21 Aug 2018 19:05:55 -0400 Subject: [PATCH 1/3] Manually submitting the file from dev branch --- PySimpleGUI.py | 84 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 69 insertions(+), 15 deletions(-) diff --git a/PySimpleGUI.py b/PySimpleGUI.py index d20da4b4..8cbed783 100644 --- a/PySimpleGUI.py +++ b/PySimpleGUI.py @@ -236,6 +236,8 @@ class InputText(Element): super().__init__(ELEM_TYPE_INPUT_TEXT, scale=scale, size=size, auto_size_text=auto_size_text, background_color=bg, text_color=fg, key=key) + def Update(self, new_value): + self.TKStringVar.set(new_value) def __del__(self): super().__del__() @@ -450,7 +452,7 @@ class Text(Element): ''' self.DisplayText = text self.TextColor = text_color if text_color else DEFAULT_TEXT_COLOR - self.Justification = justification if justification else DEFAULT_TEXT_JUSTIFICATION + self.Justification = justification if background_color is None: bg = DEFAULT_TEXT_ELEMENT_BACKGROUND_COLOR else: @@ -767,7 +769,10 @@ class Image(Element): if filename is not None: image = tk.PhotoImage(file=filename) elif data is not None: - image = tk.PhotoImage(data=data) + if type(data) is bytes: + image = tk.PhotoImage(data=data) + else: + image = data else: return self.tktext_label.configure(image=image) self.tktext_label.image = image @@ -862,7 +867,7 @@ class FlexForm: ''' Display a user defined for and return the filled in data ''' - def __init__(self, title, default_element_size=(DEFAULT_ELEMENT_SIZE[0], DEFAULT_ELEMENT_SIZE[1]), auto_size_text=None, auto_size_buttons=None, scale=(None, None), location=(None, None), button_color=None, font=None, progress_bar_color=(None, None), background_color=None, is_tabbed_form=False, border_depth=None, auto_close=False, auto_close_duration=DEFAULT_AUTOCLOSE_TIME, icon=DEFAULT_WINDOW_ICON, return_keyboard_events=False): + def __init__(self, title, default_element_size=(DEFAULT_ELEMENT_SIZE[0], DEFAULT_ELEMENT_SIZE[1]), auto_size_text=None, auto_size_buttons=None, scale=(None, None), location=(None, None), button_color=None, font=None, progress_bar_color=(None, None), background_color=None, is_tabbed_form=False, border_depth=None, auto_close=False, auto_close_duration=DEFAULT_AUTOCLOSE_TIME, icon=DEFAULT_WINDOW_ICON, return_keyboard_events=False, use_default_focus=True, text_justification=None): self.AutoSizeText = auto_size_text if auto_size_text is not None else DEFAULT_AUTOSIZE_TEXT self.AutoSizeButtons = auto_size_buttons if auto_size_buttons is not None else DEFAULT_AUTOSIZE_BUTTONS self.Title = title @@ -895,8 +900,10 @@ class FlexForm: self.DictionaryKeyCounter = 0 self.LastButtonClicked = None self.UseDictionary = False - self.UseDefaultFocus = False + self.UseDefaultFocus = use_default_focus self.ReturnKeyboardEvents = return_keyboard_events + self.LastKeyboardEvent = None + self.TextJustification = text_justification # ------------------------- Add ONE Row to Form ------------------------- # def AddRow(self, *args): @@ -953,10 +960,15 @@ class FlexForm: except: pass - if not found_focus: + if not found_focus and self.UseDefaultFocus: self.UseDefaultFocus = True + else: + self.UseDefaultFocus = False # -=-=-=-=-=-=-=-=- RUN the GUI -=-=-=-=-=-=-=-=- ## StartupTK(self) + # If a button or keyboard event happened but no results have been built, build the results + if self.LastKeyboardEvent is not None or self.LastButtonClicked is not None: + return BuildResults(self, False, self) return self.ReturnValues # ------------------------- SetIcon - set the window's fav icon ------------------------- # @@ -994,11 +1006,18 @@ class FlexForm: if not self.Shown: self.Show() else: + InitializeResults(self) self.TKroot.mainloop() if self.RootNeedsDestroying: self.TKroot.destroy() _my_windows.Decrement() - return BuildResults(self, False, self) + # if self.ReturnValues[0] is not None: # keyboard events build their own return values + # return self.ReturnValues + if self.LastKeyboardEvent is not None or self.LastButtonClicked is not None: + return BuildResults(self, False, self) + else: + return self.ReturnValues + def ReadNonBlocking(self, Message=''): if self.TKrootDestroyed: @@ -1015,14 +1034,31 @@ class FlexForm: return BuildResults(self, False, self) def KeyboardCallback(self, event ): - print("pressed", event) + self.LastButtonClicked = None + self.FormRemainedOpen = True + if event.char != '': + self.LastKeyboardEvent = event.char + else: + self.LastKeyboardEvent = str(event.keysym) + ':' + str(event.keycode) + if not self.NonBlocking: + BuildResults(self, False, self) + self.TKroot.quit() + + def MouseWheelCallback(self, event ): + self.LastButtonClicked = None + self.FormRemainedOpen = True + self.LastKeyboardEvent = 'MouseWheel:' + 'Down' if event.delta < 0 else 'Up' + if not self.NonBlocking: + BuildResults(self, False, self) + self.TKroot.quit() + def _Close(self): try: self.TKroot.update() except: pass if not self.NonBlocking: - results = BuildResults(self, False, self) + BuildResults(self, False, self) if self.TKrootDestroyed: return None self.TKrootDestroyed = True @@ -1128,7 +1164,7 @@ def FileSaveAs(target=(ThisRow, -1), file_types=(("ALL Files", "*.*"),), button_ # ------------------------- SAVE AS Element lazy function ------------------------- # def SaveAs(target=(ThisRow, -1), file_types=(("ALL Files", "*.*"),), button_text='Save As...', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None): - return Button(BUTTON_TYPE_BROWSE_FILE, target, button_text=button_text, file_types=file_types, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font) + return Button(BUTTON_TYPE_SAVEAS_FILE, target, button_text=button_text, file_types=file_types, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font) # ------------------------- SAVE BUTTON Element lazy function ------------------------- # def Save(button_text='Save', scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, bind_return_key=True,font=None, focus=False): @@ -1247,7 +1283,7 @@ def BuildResultsForSubform(form, initialize_only, top_level_form): if not initialize_only: if element.Type == ELEM_TYPE_INPUT_TEXT: value=element.TKStringVar.get() - if not top_level_form.NonBlocking and not element.do_not_clear: + if not top_level_form.NonBlocking and not element.do_not_clear and not top_level_form.ReturnKeyboardEvents: element.TKStringVar.set('') elif element.Type == ELEM_TYPE_INPUT_CHECKBOX: value = element.TKIntVar.get() @@ -1282,7 +1318,7 @@ def BuildResultsForSubform(form, initialize_only, top_level_form): elif element.Type == ELEM_TYPE_INPUT_MULTILINE: try: value=element.TKText.get(1.0, tk.END) - if not top_level_form.NonBlocking and not element.do_not_clear: + if not top_level_form.NonBlocking and not element.do_not_clear and not top_level_form.ReturnKeyboardEvents: element.TKText.delete('1.0', tk.END) except: value = None @@ -1295,6 +1331,10 @@ def BuildResultsForSubform(form, initialize_only, top_level_form): AddToReturnList(form, value) AddToReturnDictionary(top_level_form, element, value) + if form.ReturnKeyboardEvents and form.LastKeyboardEvent is not None: + button_pressed_text = form.LastKeyboardEvent + form.LastKeyboardEvent = None + try: form.ReturnValuesDictionary.pop(None, None) # clean up dictionary include None was included except: pass @@ -1383,8 +1423,14 @@ def PackFormIntoFrame(form, containing_frame, toplevel_form): stringvar.set(display_text) if auto_size_text: width = 0 - justify = tk.LEFT if element.Justification == 'left' else tk.CENTER if element.Justification == 'center' else tk.RIGHT - anchor = tk.NW if element.Justification == 'left' else tk.N if element.Justification == 'center' else tk.NE + if element.Justification is not None: + justification = element.Justification + elif toplevel_form.TextJustification is not None: + justification = toplevel_form.TextJustification + else: + justification = DEFAULT_TEXT_JUSTIFICATION + justify = tk.LEFT if justification == 'left' else tk.CENTER if justification == 'center' else tk.RIGHT + anchor = tk.NW if justification == 'left' else tk.N if justification == 'center' else tk.NE tktext_label = tk.Label(tk_row_frame, textvariable=stringvar, width=width, height=height, justify=justify, bd=border_depth) # tktext_label = tk.Label(tk_row_frame,anchor=tk.NW, text=display_text, width=width, height=height, justify=tk.LEFT, bd=border_depth) # Set wrap-length for text (in PIXELS) == PAIN IN THE ASS @@ -1768,8 +1814,12 @@ def StartupTK(my_flex_form): # root.bind('', MyFlexForm.DestroyedCallback()) ConvertFlexToTK(my_flex_form) my_flex_form.SetIcon(my_flex_form.WindowIcon) - if my_flex_form.ReturnKeyboardEvents: + if my_flex_form.ReturnKeyboardEvents and not my_flex_form.NonBlocking: + root.bind("", my_flex_form.KeyboardCallback) + root.bind("", my_flex_form.MouseWheelCallback) + elif my_flex_form.ReturnKeyboardEvents: root.bind("", my_flex_form.KeyboardCallback) + root.bind("", my_flex_form.MouseWheelCallback) if my_flex_form.AutoClose: duration = DEFAULT_AUTOCLOSE_TIME if my_flex_form.AutoCloseDuration is None else my_flex_form.AutoCloseDuration @@ -2576,11 +2626,15 @@ def ChangeLookAndFeel(index): sprint=ScrolledTextBox # Converts an object's contents into a nice printable string. Great for dumping debug data -def ObjToString_old(obj): +def ObjToStringSingleObj(obj): + if obj is None: + return 'None' return str(obj.__class__) + '\n' + '\n'.join( (repr(item) + ' = ' + repr(obj.__dict__[item]) for item in sorted(obj.__dict__))) def ObjToString(obj, extra=' '): + if obj is None: + return 'None' return str(obj.__class__) + '\n' + '\n'.join( (extra + (str(item) + ' = ' + (ObjToString(obj.__dict__[item], extra + ' ') if hasattr(obj.__dict__[item], '__dict__') else str( From c765f1f6f754fe8d6a6fd3321c5dd474c9ebf2e8 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Tue, 21 Aug 2018 19:10:26 -0400 Subject: [PATCH 2/3] Manually moving from Dev Latest on PC --- Demo_Keyboard.py | 17 +++++++---------- Demo_Keyboard_Realtime.py | 15 ++++++--------- Demo_PDF_Viewer.py | 4 +++- PySimpleGUI.py | 16 ---------------- 4 files changed, 16 insertions(+), 36 deletions(-) diff --git a/Demo_Keyboard.py b/Demo_Keyboard.py index 85f63b8a..88c5fc5d 100644 --- a/Demo_Keyboard.py +++ b/Demo_Keyboard.py @@ -1,26 +1,23 @@ -import sys import PySimpleGUI as sg # Recipe for getting keys, one at a time as they are released # If want to use the space bar, then be sure and disable the "default focus" -with sg.FlexForm('Realtime Keyboard Test', return_keyboard_events=True, use_default_focus=False) as form: - text_elem = sg.Text('', size=(12,1)) - layout = [[sg.Text('Press a key')], +with sg.FlexForm("Keyboard Test", return_keyboard_events=True, use_default_focus=False) as form: + text_elem = sg.Text("", size=(18,1)) + layout = [[sg.Text("Press a key or scroll mouse")], [text_elem], - [sg.SimpleButton('OK')]] + [sg.SimpleButton("OK")]] form.Layout(layout) # ---===--- Loop taking in user input --- # while True: - button, value = form.Read() + button, value = form.ReadNonBlocking() - if button == 'OK': - print(button, 'exiting') + if button == "OK" or (button is None and value is None): + print(button, "exiting") break if button is not None: text_elem.Update(button) - else: - break diff --git a/Demo_Keyboard_Realtime.py b/Demo_Keyboard_Realtime.py index bb1d145e..25812024 100644 --- a/Demo_Keyboard_Realtime.py +++ b/Demo_Keyboard_Realtime.py @@ -1,19 +1,16 @@ import PySimpleGUI as sg -# Recipe for getting a continuous stream of keys when using a non-blocking form -# If want to use the space bar, then be sure and disable the "default focus" - -with sg.FlexForm('Realtime Keyboard Test', return_keyboard_events=True, use_default_focus=False) as form: - layout = [[sg.Text('Hold down a key')], - [sg.SimpleButton('OK')]] +with sg.FlexForm("Realtime Keyboard Test", return_keyboard_events=True, use_default_focus=False) as form: + layout = [[sg.Text("Hold down a key")], + [sg.SimpleButton("OK")]] form.Layout(layout) - # ---===--- Loop taking in user input --- # + while True: button, value = form.ReadNonBlocking() - if button == 'OK': - print(button, value, 'exiting') + if button == "OK": + print(button, value, "exiting") break if button is not None: print(button) diff --git a/Demo_PDF_Viewer.py b/Demo_PDF_Viewer.py index ecd60c9c..6fd5fb42 100644 --- a/Demo_PDF_Viewer.py +++ b/Demo_PDF_Viewer.py @@ -37,6 +37,8 @@ import fitz import PySimpleGUI as sg from binascii import hexlify +sg.ChangeLookAndFeel('GreenTan') + if len(sys.argv) == 1: rc, fname = sg.GetFileBox('PDF Browser', 'PDF file to open', file_types=(("PDF Files", "*.pdf"),)) if rc is False: @@ -126,7 +128,7 @@ while True: if button is None: continue - if button in ("Escape:27"): # this spares me a 'Quit' button! + if button in ("Escape:27",): # this spares me a 'Quit' button! break # print("hex(button)", hexlify(button.encode())) if button[0] == chr(13): # surprise: this is 'Enter'! diff --git a/PySimpleGUI.py b/PySimpleGUI.py index ffdec2b7..8cbed783 100644 --- a/PySimpleGUI.py +++ b/PySimpleGUI.py @@ -867,10 +867,6 @@ class FlexForm: ''' Display a user defined for and return the filled in data ''' -<<<<<<< HEAD -======= - ->>>>>>> 531b32ab66746c9f4b6acd2ea8b6d113cb235827 def __init__(self, title, default_element_size=(DEFAULT_ELEMENT_SIZE[0], DEFAULT_ELEMENT_SIZE[1]), auto_size_text=None, auto_size_buttons=None, scale=(None, None), location=(None, None), button_color=None, font=None, progress_bar_color=(None, None), background_color=None, is_tabbed_form=False, border_depth=None, auto_close=False, auto_close_duration=DEFAULT_AUTOCLOSE_TIME, icon=DEFAULT_WINDOW_ICON, return_keyboard_events=False, use_default_focus=True, text_justification=None): self.AutoSizeText = auto_size_text if auto_size_text is not None else DEFAULT_AUTOSIZE_TEXT self.AutoSizeButtons = auto_size_buttons if auto_size_buttons is not None else DEFAULT_AUTOSIZE_BUTTONS @@ -1038,10 +1034,6 @@ class FlexForm: return BuildResults(self, False, self) def KeyboardCallback(self, event ): -<<<<<<< HEAD -======= - ->>>>>>> 531b32ab66746c9f4b6acd2ea8b6d113cb235827 self.LastButtonClicked = None self.FormRemainedOpen = True if event.char != '': @@ -1060,10 +1052,6 @@ class FlexForm: BuildResults(self, False, self) self.TKroot.quit() -<<<<<<< HEAD -======= - ->>>>>>> 531b32ab66746c9f4b6acd2ea8b6d113cb235827 def _Close(self): try: @@ -1826,10 +1814,6 @@ def StartupTK(my_flex_form): # root.bind('', MyFlexForm.DestroyedCallback()) ConvertFlexToTK(my_flex_form) my_flex_form.SetIcon(my_flex_form.WindowIcon) -<<<<<<< HEAD -======= - ->>>>>>> 531b32ab66746c9f4b6acd2ea8b6d113cb235827 if my_flex_form.ReturnKeyboardEvents and not my_flex_form.NonBlocking: root.bind("", my_flex_form.KeyboardCallback) root.bind("", my_flex_form.MouseWheelCallback) From 1b0f7488d0bec9561cdb6609496fdace793ca393 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Tue, 21 Aug 2018 20:21:27 -0400 Subject: [PATCH 3/3] Fix mousewheel up bug --- PySimpleGUI.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PySimpleGUI.py b/PySimpleGUI.py index 8cbed783..5b8202ac 100644 --- a/PySimpleGUI.py +++ b/PySimpleGUI.py @@ -1047,7 +1047,7 @@ class FlexForm: def MouseWheelCallback(self, event ): self.LastButtonClicked = None self.FormRemainedOpen = True - self.LastKeyboardEvent = 'MouseWheel:' + 'Down' if event.delta < 0 else 'Up' + self.LastKeyboardEvent = 'MouseWheel:Down' if event.delta < 0 else 'MouseWheel:Up' if not self.NonBlocking: BuildResults(self, False, self) self.TKroot.quit()