diff --git a/extras/README.md b/extras/README.md index 0ab1e12..25de326 100644 --- a/extras/README.md +++ b/extras/README.md @@ -67,7 +67,7 @@ Constructor args: Methods: * `show` Draw the grid lines to the framebuffer. - * `__getitem__` Return a list containing one or more `Label` instances. + * `__getitem__` Returns an iterator enabling `Label` instances to be accessed. * `__setitem__` Assign a value to one or more labels. If multiple labels are specified and a single text value is passed, all labels will receive that value. If an iterator is passed, consecutive labels will receive values from @@ -96,6 +96,11 @@ grid[1:3, 1:3] = (str(n) for n in range(2)) # Produces # 0 1 # 1 1 ``` +Read access: +```python +for label in grid[2, 0:]: + v = label.value() # Access each label in row 2 +``` Sample usage (complete example): ```python from color_setup import ssd diff --git a/extras/widgets/grid.py b/extras/widgets/grid.py index 5edb6a6..125a149 100644 --- a/extras/widgets/grid.py +++ b/extras/widgets/grid.py @@ -34,12 +34,10 @@ class Grid(DObject): r += self.cheight c = col - def __getitem__(self, *args): # Return the Label instance + def __getitem__(self, *args): indices = do_args(args, self.nrows, self.ncols) - res = [] for i in indices: - res.append(self.cells[i]) - return res + yield self.cells[i] # allow grid[r, c] = "foo" or kwargs for Label: # grid[r, c] = {"text": str(n), "fgcolor" : RED}