From 96635ef5a29c9d1f38d7f4ce74b66a0be6de3fb9 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Sat, 29 Sep 2018 02:03:07 -0400 Subject: [PATCH] New window methods - Hide, UnHide! Fix for menu letter underlining, Removed manager from Popups --- PySimpleGUI.py | 187 +++++++++++++++++++++++++------------------------ 1 file changed, 96 insertions(+), 91 deletions(-) diff --git a/PySimpleGUI.py b/PySimpleGUI.py index 172b588c..c538e8e7 100644 --- a/PySimpleGUI.py +++ b/PySimpleGUI.py @@ -2353,6 +2353,12 @@ class Window: def Enable(self): self.TKroot.grab_release() + def Hide(self): + self.TKroot.withdraw() + + def UnHide(self): + self.TKroot.deiconify() + def __enter__(self): return self @@ -2760,18 +2766,14 @@ def AddMenuItem(top_menu, sub_menu_info, element, is_sub_menu=False, skip=False) if type(sub_menu_info) is str: if not is_sub_menu and not skip: # print(f'Adding command {sub_menu_info}') - pos = sub_menu_info.find('_&') + pos = sub_menu_info.find('&') if pos != -1: - _ = sub_menu_info[:pos] - try: - _ += sub_menu_info[pos+2:] - except Exception as e: - print(e) - sub_menu_info = _ + if pos == 0 or sub_menu_info[pos-1] != "\\": + sub_menu_info = sub_menu_info[:pos] + sub_menu_info[pos+1:] if sub_menu_info == '---': top_menu.add('separator') else: - top_menu.add_command(label=sub_menu_info, underline=pos-1, command=lambda: Menu.MenuItemChosenCallback(element, sub_menu_info)) + top_menu.add_command(label=sub_menu_info, underline=pos, command=lambda: Menu.MenuItemChosenCallback(element, sub_menu_info)) else: i = 0 while i < (len(sub_menu_info)): @@ -3246,17 +3248,14 @@ def PackFormIntoFrame(form, containing_frame, toplevel_form): element.TKMenu = tk.Menu(toplevel_form.TKroot, tearoff=element.Tearoff) # create the menubar menubar = element.TKMenu for menu_entry in menu_def: - # print(f'Adding a Menubar ENTRY') + # print(f'Adding a Menubar ENTRY {menu_entry}') baritem = tk.Menu(menubar, tearoff=element.Tearoff) - pos = menu_entry[0].find('_&') + pos = menu_entry[0].find('&') + # print(pos) if pos != -1: - _ = menu_entry[0][:pos] - try: - _ += menu_entry[0][pos+2:] - except: - pass - menu_entry[0] = _ - menubar.add_cascade(label=menu_entry[0], menu=baritem, underline = pos-1) + if pos == 0 or menu_entry[0][pos-1] != "\\": + menu_entry[0] = menu_entry[0][:pos] + menu_entry[0][pos+1:] + menubar.add_cascade(label=menu_entry[0], menu=baritem, underline = pos) if len(menu_entry) > 1: AddMenuItem(baritem, menu_entry[1], element) toplevel_form.TKroot.configure(menu=element.TKMenu) @@ -3947,18 +3946,20 @@ def PopupGetFolder(message, default_path='', no_window=False, size=(None,None), root.destroy() return folder_name - with Window(title=message, icon=icon, auto_size_text=True, button_color=button_color, background_color=background_color, - font=font, no_titlebar=no_titlebar, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location) as form: - layout = [[Text(message, auto_size_text=True, text_color=text_color, background_color=background_color)], - [InputText(default_text=default_path, size=size), FolderBrowse()], - [Ok(), Cancel()]] + layout = [[Text(message, auto_size_text=True, text_color=text_color, background_color=background_color)], + [InputText(default_text=default_path, size=size), FolderBrowse()], + [Ok(), Cancel()]] - (button, input_values) = form.LayoutAndRead(layout) - if button != 'Ok': - return None - else: - path = input_values[0] - return path + window = Window(title=message, icon=icon, auto_size_text=True, button_color=button_color, background_color=background_color, + font=font, no_titlebar=no_titlebar, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location) + + (button, input_values) = window.LayoutAndRead(layout) + + if button != 'Ok': + return None + else: + path = input_values[0] + return path ##################################### # PopupGetFile # @@ -3999,18 +4000,19 @@ def PopupGetFile(message, default_path='', default_extension='', save_as=False, browse_button = SaveAs(file_types=file_types) if save_as else FileBrowse(file_types=file_types) - with Window(title=message, icon=icon, auto_size_text=True, button_color=button_color, font=font, background_color=background_color, - no_titlebar=no_titlebar, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location) as form: - layout = [[Text(message, auto_size_text=True, text_color=text_color, background_color=background_color)], - [InputText(default_text=default_path, size=size), browse_button], - [Ok(), Cancel()]] + layout = [[Text(message, auto_size_text=True, text_color=text_color, background_color=background_color)], + [InputText(default_text=default_path, size=size), browse_button], + [Ok(), Cancel()]] - (button, input_values) = form.LayoutAndRead(layout) - if button != 'Ok': - return None - else: - path = input_values[0] - return path + window = Window(title=message, icon=icon, auto_size_text=True, button_color=button_color, font=font, background_color=background_color, + no_titlebar=no_titlebar, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location) + + (button, input_values) = window.Layout(layout).Read() + if button != 'Ok': + return None + else: + path = input_values[0] + return path ##################################### # PopupGetText # @@ -4033,17 +4035,20 @@ def PopupGetText(message, default_text='', password_char='', size=(None,None), b :param location: :return: Text entered or None if window was closed """ - with Window(title=message, icon=icon, auto_size_text=True, button_color=button_color, no_titlebar=no_titlebar, - background_color=background_color, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location) as form: - layout = [[Text(message, auto_size_text=True, text_color=text_color, background_color=background_color, font=font)], - [InputText(default_text=default_text, size=size, password_char=password_char)], - [Ok(), Cancel()]] - (button, input_values) = form.LayoutAndRead(layout) - if button != 'Ok': - return None - else: - return input_values[0] + layout = [[Text(message, auto_size_text=True, text_color=text_color, background_color=background_color, font=font)], + [InputText(default_text=default_text, size=size, password_char=password_char)], + [Ok(), Cancel()]] + + window = Window(title=message, icon=icon, auto_size_text=True, button_color=button_color, no_titlebar=no_titlebar, + background_color=background_color, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location) + + (button, input_values) = window.Layout(layout).Read() + + if button != 'Ok': + return None + else: + return input_values[0] # ============================== SetGlobalIcon ======# @@ -4389,50 +4394,50 @@ def Popup(*args, button_color=None, background_color=None, text_color=None, butt else: local_line_width = MESSAGE_BOX_LINE_WIDTH title = args_to_print[0] if args_to_print[0] is not None else 'None' - with Window(title, auto_size_text=True, background_color=background_color, button_color=button_color, auto_close=auto_close, auto_close_duration=auto_close_duration, icon=icon, font=font, no_titlebar=no_titlebar, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location) as form: - max_line_total, total_lines = 0,0 - for message in args_to_print: - # fancy code to check if string and convert if not is not need. Just always convert to string :-) - # if not isinstance(message, str): message = str(message) - message = str(message) - if message.count('\n'): - message_wrapped = message - else: - message_wrapped = textwrap.fill(message, local_line_width) - message_wrapped_lines = message_wrapped.count('\n')+1 - longest_line_len = max([len(l) for l in message.split('\n')]) - width_used = min(longest_line_len, local_line_width) - max_line_total = max(max_line_total, width_used) - # height = _GetNumLinesNeeded(message, width_used) - height = message_wrapped_lines - form.AddRow(Text(message_wrapped, auto_size_text=True, text_color=text_color, background_color=background_color)) - total_lines += height + form = Window(title, auto_size_text=True, background_color=background_color, button_color=button_color, auto_close=auto_close, auto_close_duration=auto_close_duration, icon=icon, font=font, no_titlebar=no_titlebar, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location) + max_line_total, total_lines = 0,0 + for message in args_to_print: + # fancy code to check if string and convert if not is not need. Just always convert to string :-) + # if not isinstance(message, str): message = str(message) + message = str(message) + if message.count('\n'): + message_wrapped = message + else: + message_wrapped = textwrap.fill(message, local_line_width) + message_wrapped_lines = message_wrapped.count('\n')+1 + longest_line_len = max([len(l) for l in message.split('\n')]) + width_used = min(longest_line_len, local_line_width) + max_line_total = max(max_line_total, width_used) + # height = _GetNumLinesNeeded(message, width_used) + height = message_wrapped_lines + form.AddRow(Text(message_wrapped, auto_size_text=True, text_color=text_color, background_color=background_color)) + total_lines += height - pad = max_line_total-15 if max_line_total > 15 else 1 - pad =1 - if non_blocking: - PopupButton = DummyButton - else: - PopupButton = SimpleButton - # show either an OK or Yes/No depending on paramater - if button_type is POPUP_BUTTONS_YES_NO: - form.AddRow(Text('', size=(pad, 1), auto_size_text=False, text_color=text_color, background_color=background_color), PopupButton('Yes', button_color=button_color, focus=True, bind_return_key=True), PopupButton('No', button_color=button_color)) - elif button_type is POPUP_BUTTONS_CANCELLED: - form.AddRow(Text('', size=(pad, 1), auto_size_text=False, text_color=text_color, background_color=background_color), PopupButton('Cancelled', button_color=button_color, focus=True, bind_return_key=True)) - elif button_type is POPUP_BUTTONS_ERROR: - form.AddRow(Text('', size=(pad, 1), auto_size_text=False, text_color=text_color, background_color=background_color), PopupButton('Error', size=(6, 1), button_color=button_color, focus=True, bind_return_key=True)) - elif button_type is POPUP_BUTTONS_OK_CANCEL: - form.AddRow(Text('', size=(pad, 1), auto_size_text=False, text_color=text_color, background_color=background_color), PopupButton('OK', size=(5, 1), button_color=button_color, focus=True, bind_return_key=True), - PopupButton('Cancel', size=(5, 1), button_color=button_color)) - elif button_type is POPUP_BUTTONS_NO_BUTTONS: - pass - else: - form.AddRow(Text('', size=(pad, 1), auto_size_text=False, background_color=background_color), PopupButton('OK', size=(5, 1), button_color=button_color, focus=True, bind_return_key=True)) + pad = max_line_total-15 if max_line_total > 15 else 1 + pad =1 + if non_blocking: + PopupButton = DummyButton + else: + PopupButton = SimpleButton + # show either an OK or Yes/No depending on paramater + if button_type is POPUP_BUTTONS_YES_NO: + form.AddRow(Text('', size=(pad, 1), auto_size_text=False, text_color=text_color, background_color=background_color), PopupButton('Yes', button_color=button_color, focus=True, bind_return_key=True), PopupButton('No', button_color=button_color)) + elif button_type is POPUP_BUTTONS_CANCELLED: + form.AddRow(Text('', size=(pad, 1), auto_size_text=False, text_color=text_color, background_color=background_color), PopupButton('Cancelled', button_color=button_color, focus=True, bind_return_key=True)) + elif button_type is POPUP_BUTTONS_ERROR: + form.AddRow(Text('', size=(pad, 1), auto_size_text=False, text_color=text_color, background_color=background_color), PopupButton('Error', size=(6, 1), button_color=button_color, focus=True, bind_return_key=True)) + elif button_type is POPUP_BUTTONS_OK_CANCEL: + form.AddRow(Text('', size=(pad, 1), auto_size_text=False, text_color=text_color, background_color=background_color), PopupButton('OK', size=(5, 1), button_color=button_color, focus=True, bind_return_key=True), + PopupButton('Cancel', size=(5, 1), button_color=button_color)) + elif button_type is POPUP_BUTTONS_NO_BUTTONS: + pass + else: + form.AddRow(Text('', size=(pad, 1), auto_size_text=False, background_color=background_color), PopupButton('OK', size=(5, 1), button_color=button_color, focus=True, bind_return_key=True)) - if non_blocking: - button, values = form.ReadNonBlocking() - else: - button, values = form.Show() + if non_blocking: + button, values = form.ReadNonBlocking() + else: + button, values = form.Show() return button