Grid widget __getitem__() returns iterator.

encoder_driver
peterhinch 2023-06-12 14:44:24 +01:00
rodzic 310957e518
commit d6cc79e714
2 zmienionych plików z 8 dodań i 6 usunięć

Wyświetl plik

@ -1127,7 +1127,7 @@ Constructor args:
in the `Label` as defined by `lwidth`. in the `Label` as defined by `lwidth`.
Method: Method:
* `__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 * `__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 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 value. If an iterator is passed, consecutive labels will receive values from
@ -1156,7 +1156,11 @@ grid[1:3, 1:3] = (str(n) for n in range(2)) # Produces
# 0 1 # 0 1
# 1 1 # 1 1
``` ```
Read access:
```python
for label in grid[2, 0:]:
v = label.value() # Access text of each label in row 2
```
Example uses: Example uses:
```python ```python
colwidth = (20, 30) # Col 0 width is 20, subsequent columns 30 colwidth = (20, 30) # Col 0 width is 20, subsequent columns 30

Wyświetl plik

@ -46,12 +46,10 @@ class Grid(Widget):
r += self.cheight r += self.cheight
c = col c = col
def __getitem__(self, *args): # Return the Label instance def __getitem__(self, *args):
indices = do_args(args, self.nrows, self.ncols) indices = do_args(args, self.nrows, self.ncols)
res = []
for i in indices: for i in indices:
res.append(self.cells[i]) yield self.cells[i]
return res
# allow grid[[r, c]] = "foo" or kwargs for Label: # allow grid[[r, c]] = "foo" or kwargs for Label:
# grid[[r, c]] = {"text": str(n), "fgcolor" : RED} # grid[[r, c]] = {"text": str(n), "fgcolor" : RED}