MCPcopy Index your code
hub / github.com/PySimpleGUI/PySimpleGUI / update

Method update

PySimpleGUI/PySimpleGUI.py:8621–8684  ·  view source on GitHub ↗

Update a menubar - can change the menu definition and visibility. The entire menu has to be specified Changes will not be visible in your window until you call window.read or window.refresh. If you change visibility, your element may MOVE. If you want it to remain station

(self, menu_definition=None, visible=None)

Source from the content-addressed store, hash-verified

8619 _exit_mainloop(self.ParentForm, self)
8620
8621 def update(self, menu_definition=None, visible=None):
8622 """
8623 Update a menubar - can change the menu definition and visibility. The entire menu has to be specified
8624
8625 Changes will not be visible in your window until you call window.read or window.refresh.
8626
8627 If you change visibility, your element may MOVE. If you want it to remain stationary, use the "layout helper"
8628 function "pin" to ensure your element is "pinned" to that location in your layout so that it returns there
8629 when made visible.
8630
8631 :param menu_definition: The menu definition list
8632 :type menu_definition: List[List[Tuple[str, List[str]]]
8633 :param visible: control visibility of element
8634 :type visible: (bool)
8635 """
8636 if not self._widget_was_created(): # if widget hasn't been created yet, then don't allow
8637 return
8638
8639 if self._this_elements_window_closed():
8640 _error_popup_with_traceback('Error in Menu.update - The window was closed')
8641 return
8642
8643 if menu_definition is not None:
8644 self.MenuDefinition = copy.deepcopy(menu_definition)
8645 if self.TKMenu is None: # if no menu exists, make one
8646 self.TKMenu = tk.Menu(self.ParentForm.TKroot, tearoff=self.Tearoff, tearoffcommand=self._tearoff_menu_callback) # create the menubar
8647 menubar = self.TKMenu
8648 # Delete all the menu items (assuming 10000 should be a high enough number to cover them all)
8649 menubar.delete(0, 10000)
8650 self.Widget = self.TKMenu # same the new menu so user can access to extend PySimpleGUI
8651 for menu_entry in self.MenuDefinition:
8652 baritem = tk.Menu(menubar, tearoff=self.Tearoff, tearoffcommand=self._tearoff_menu_callback)
8653
8654 if self.BackgroundColor not in (COLOR_SYSTEM_DEFAULT, None):
8655 baritem.config(bg=self.BackgroundColor)
8656 if self.TextColor not in (COLOR_SYSTEM_DEFAULT, None):
8657 baritem.config(fg=self.TextColor)
8658 if self.DisabledTextColor not in (COLOR_SYSTEM_DEFAULT, None):
8659 baritem.config(disabledforeground=self.DisabledTextColor)
8660 if self.Font is not None:
8661 baritem.config(font=self.Font)
8662
8663 if self.Font is not None:
8664 baritem.config(font=self.Font)
8665 pos = menu_entry[0].find(MENU_SHORTCUT_CHARACTER)
8666 # print(pos)
8667 if pos != -1:
8668 if pos == 0 or menu_entry[0][pos - len(MENU_SHORTCUT_CHARACTER)] != "\\":
8669 menu_entry[0] = menu_entry[0][:pos] + menu_entry[0][pos + len(MENU_SHORTCUT_CHARACTER):]
8670 if menu_entry[0][0] == MENU_DISABLED_CHARACTER:
8671 menubar.add_cascade(label=menu_entry[0][len(MENU_DISABLED_CHARACTER):], menu=baritem, underline=pos)
8672 menubar.entryconfig(menu_entry[0][len(MENU_DISABLED_CHARACTER):], state='disabled')
8673 else:
8674 menubar.add_cascade(label=menu_entry[0], menu=baritem, underline=pos)
8675
8676 if len(menu_entry) > 1:
8677 AddMenuItem(baritem, menu_entry[1], self)
8678

Callers

nothing calls this directly

Calls 4

AddMenuItemFunction · 0.85
_widget_was_createdMethod · 0.80

Tested by

no test coverage detected