From f250ef5a66b4536da0f2b75de86956e1d06f1a96 Mon Sep 17 00:00:00 2001 From: Jakub Wlodek Date: Thu, 20 Apr 2023 12:09:58 -0400 Subject: [PATCH] Allow for getting/setting item by index in menu widgets --- py_cui/ui.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/py_cui/ui.py b/py_cui/ui.py index 06ae250..56d3d1c 100644 --- a/py_cui/ui.py +++ b/py_cui/ui.py @@ -961,6 +961,41 @@ def set_selected_item(self, selected_item: Any): self._view_items[self._selected_item] = selected_item + def get_item_index(self, index: int): + """Function that returns reference to item at given index + + Paramters + --------- + index : int + Index of object + + Returns + ------- + item : Any + Item at specified index in the list, or None if index is invalid. + """ + + try: + return self._view_items[index] + except IndexError: + return None + + + def set_item_index(self, item: Any, index: int): + """Function that sets the item at the specified index of the menu + + Parameters + ---------- + item: Any + Item to put in menu at given index + index: int + Index at which to put the specified item. + """ + + if item is not None and len(self._view_items) > index and index >= 0: + self._view_items[index] = item + + class CheckBoxMenuImplementation(MenuImplementation): """Class representing checkbox menu ui implementation