From 0e482cd7727dc71cd7e4e56484cba83d4cf2b4d9 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Sat, 10 Apr 2021 21:46:05 -0400 Subject: [PATCH] Right click menus added to Button & ButtonMenu elements MENU_RIGHT_CLICK_DISABLED constant --- PySimpleGUI.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/PySimpleGUI.py b/PySimpleGUI.py index 826022e5..5a72508a 100644 --- a/PySimpleGUI.py +++ b/PySimpleGUI.py @@ -1,5 +1,5 @@ #!/usr/bin/python3 -version = __version__ = "4.38.0.11 Unreleased\nAdded Element.block_focus to allow blocking an element from getting focus, Listbox now sets the selected colors to be opposite of normal text/background colors, added highlight parms to Listbox so that they can be directly set, gave Mac users the abliity to override the TTK-Buttons-Only rule for Macs so that if forced, a Button CAN use tk buttons on a Mac, exposed listbox_frame for Listbox so can expand a listbox, new parameter right_click_menu_tearoff parm added to Window, better line wrapping for error windows, show an error window if a bad Image specified in the Image element, expand_x & expand_y parms for vtop vbottom vcenter, added code to element.expand to handle the Listbox correctly, MENU_RIGHT_CLICK_EDITME_EXIT menu defintiion, added framework_version, fix for RealtimeButton, put back __version__, popup_menu added, s parm added to all elements, new figlets, experimental - more permissive layouts - embedded layouts for PSG+ features, symbols - double L/R & arrowheads, parameter right_click_entry_selected_colors added to Window - a simple dual color string or a tuple - used for right click menus and pop_menu, FIX for write_event_value (THANK YOU daemon2021!)" +version = __version__ = "4.38.0.12 Unreleased\nAdded Element.block_focus to allow blocking an element from getting focus, Listbox now sets the selected colors to be opposite of normal text/background colors, added highlight parms to Listbox so that they can be directly set, gave Mac users the abliity to override the TTK-Buttons-Only rule for Macs so that if forced, a Button CAN use tk buttons on a Mac, exposed listbox_frame for Listbox so can expand a listbox, new parameter right_click_menu_tearoff parm added to Window, better line wrapping for error windows, show an error window if a bad Image specified in the Image element, expand_x & expand_y parms for vtop vbottom vcenter, added code to element.expand to handle the Listbox correctly, MENU_RIGHT_CLICK_EDITME_EXIT menu defintiion, added framework_version, fix for RealtimeButton, put back __version__, popup_menu added, s parm added to all elements, new figlets, experimental - more permissive layouts - embedded layouts for PSG+ features, symbols - double L/R & arrowheads, parameter right_click_entry_selected_colors added to Window - a simple dual color string or a tuple - used for right click menus and pop_menu, FIX for write_event_value (THANK YOU daemon2021!), right click menu added to button and buttonmenu elements, constant MENU_RIGHT_CLICK_DISABLED [[]]to block element from using the window's right click menu" __version__ = version.split()[0] # For PEP 396 and PEP 345 @@ -488,6 +488,7 @@ MENU_DISABLED_CHARACTER = '!' MENU_SHORTCUT_CHARACTER = '&' MENU_KEY_SEPARATOR = '::' MENU_RIGHT_CLICK_EDITME_EXIT = ['_', ['Edit Me', 'Exit']] +MENU_RIGHT_CLICK_DISABLED = [[]] ENABLE_TK_WINDOWS = False USE_CUSTOM_TITLEBAR = False @@ -3553,7 +3554,7 @@ class Button(Element): file_types=(("ALL Files", "*.*"),), initial_folder=None, default_extension='', disabled=False, change_submits=False, enable_events=False, image_filename=None, image_data=None, image_size=(None, None), image_subsample=None, border_width=None, size=(None, None), s=(None, None), auto_size_button=None, button_color=None, disabled_button_color=None, - highlight_colors=None, use_ttk_buttons=None, font=None, bind_return_key=False, focus=False, pad=None, key=None, k=None, visible=True, metadata=None): + highlight_colors=None, use_ttk_buttons=None, font=None, bind_return_key=False, focus=False, pad=None, key=None, k=None, right_click_menu=None, visible=True, metadata=None): """ :param button_text: Text to be displayed on the button :type button_text: (str) @@ -3569,7 +3570,7 @@ class Button(Element): :type initial_folder: (str) :param default_extension: If no extension entered by user, add this to filename (only used in saveas dialogs) :type default_extension: (str) - :param disabled: If True button will be created disabled. If BUTTON_DISABLED_MEANS_IGNORE then the button will be ignored rather than disabled using tkinger + :param disabled: If True button will be created disabled. If BUTTON_DISABLED_MEANS_IGNORE then the button will be ignored rather than disabled using tkinter :type disabled: (bool | str) :param change_submits: DO NOT USE. Only listed for backwards compat - Use enable_events instead :type change_submits: (bool) @@ -3611,6 +3612,8 @@ class Button(Element): :type key: str | int | tuple | object :param k: Same as the Key. You can use either k or key. Which ever is set will be used. :type k: str | int | tuple | object + :param right_click_menu: A list of lists of Menu items to show when this element is right clicked. See user docs for exact format. + :type right_click_menu: List[List[ List[str] | str ]] :param visible: set visibility state of the element :type visible: (bool) :param metadata: User metadata that can be set to ANYTHING @@ -3623,6 +3626,7 @@ class Button(Element): self.Widget = self.TKButton = None # type: tk.Button self.Target = target self.ButtonText = str(button_text) + self.RightClickMenu=right_click_menu # Button colors can be a tuple (text, background) or a string with format "text on background" # bc = button_color # if button_color is None: @@ -12161,6 +12165,8 @@ def PackFormIntoFrame(form, containing_frame, toplevel_form): def _add_right_click_menu(element): + if element.RightClickMenu == MENU_RIGHT_CLICK_DISABLED: + return if element.RightClickMenu or toplevel_form.RightClickMenu: menu = element.RightClickMenu or toplevel_form.RightClickMenu top_menu = tk.Menu(toplevel_form.TKroot, tearoff=toplevel_form.right_click_menu_tearoff, tearoffcommand=element._tearoff_menu_callback) @@ -12529,6 +12535,7 @@ def PackFormIntoFrame(form, containing_frame, toplevel_form): tkbutton.config(highlightcolor=element.HighlightColors[0]) except Exception as e: print('Button with text: ', btext, 'has a bad highlight color', element.HighlightColors) + _add_right_click_menu(element) # ------------------------- BUTTON placement element ttk version ------------------------- # elif element_type == ELEM_TYPE_BUTTON: @@ -12631,6 +12638,7 @@ def PackFormIntoFrame(form, containing_frame, toplevel_form): element.TKButton['state'] = 'disabled' tkbutton.configure(style=style_name) # IMPORTANT! Apply the style to the button! + _add_right_click_menu(element) if element.Tooltip is not None: element.TooltipObject = ToolTip(element.TKButton, text=element.Tooltip,