From fa41a0ac9bfa38f6a9648663dc57e375ecf49bb9 Mon Sep 17 00:00:00 2001 From: troyhy Date: Mon, 19 Aug 2024 01:35:20 +0300 Subject: [PATCH] Add set_elements method to ListBox widget Introduced a new `set_elements` method in the ListBox widget to handle initialization and validation of list items and dimensions. This method simplifies listbox element updates and ensures proper scrolling when the list exceeds display limits. --- gui/widgets/listbox.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/gui/widgets/listbox.py b/gui/widgets/listbox.py index b093cd7..39b984a 100644 --- a/gui/widgets/listbox.py +++ b/gui/widgets/listbox.py @@ -68,9 +68,34 @@ class Listbox(Widget): self.cb_args = args self.select_color = select_color self.fontcolor = fontcolor - self._value = value # No callback until user selects + self._value = value # No callback until user selects self.ev = value # Value change detection + + def set_elements(self, new_elements): + e0 = new_elements[0] + # Check whether elements specified as (str, str,...) or ([str, callback, args], [...) + if isinstance(e0, tuple) or isinstance(e0, list): + self.els = new_elements # Retain original for .despatch + self.elements = [x[0] for x in new_elements] # Copy text component + else: + # elements are strings list + self.elements = new_elements + + if any(not isinstance(s, str) for s in self.elements): + raise ValueError("Invalid elements arg.") + + # Calculate dimensions + self.entry_height, height, self.dlines, tw = self.dimensions( + self.writer, self.elements, self.dlines + ) + self.ntop = 0 + if self._value >= self.dlines: # Must scroll + self._value = min(self._value, len(new_elements) - 1) + self.ntop = self._value - self.dlines + 1 + + self.draw = True + def show(self): if not super().show(False): # Clear to self.bgcolor return