kopia lustrzana https://github.com/peterhinch/micropython-micro-gui
grid widget: Allow proper 2D array syntax.
rodzic
2c10ca4418
commit
ebfb980e94
|
@ -1101,8 +1101,7 @@ specified width +4 to allow for borders. The dimensions of the widget including
|
|||
borders are thus:
|
||||
height = no. of rows * (font height + 4)
|
||||
width = sum(column width + 4)
|
||||
Cells may be addressed as a 1-dimensional list or by a `[row, col]` 2-list or
|
||||
2-tuple.
|
||||
Cells may be addressed as a 1 or 2-dimensional array.
|
||||
|
||||
Constructor args:
|
||||
1. `writer` The `Writer` instance (font and screen) to use.
|
||||
|
@ -1137,11 +1136,11 @@ Example uses:
|
|||
colwidth = (20, 30) # Col 0 width is 20, subsequent columns 30
|
||||
self.grid = Grid(wri, row, col, colwidth, rows, cols, justify=Label.CENTRE)
|
||||
self.grid[20] = "" # Clear cell 20 by setting its value to ""
|
||||
self.grid[[2, 5]] = str(42) # Note syntax
|
||||
self.grid[2, 5] = str(42) # 2D array syntax
|
||||
d = {} # For indiviual control of cell appearance
|
||||
d["fgcolor"] = RED
|
||||
d["text"] = str(99)
|
||||
self.grid[(3, 7)] = d # Specify color as well as text
|
||||
self.grid[3, 7] = d # Specify color as well as text
|
||||
del d["fgcolor"] # Revert to default
|
||||
d["invert"] = True
|
||||
self.grid[17] = d
|
||||
|
|
|
@ -33,7 +33,7 @@ class BaseScreen(Screen):
|
|||
row = self.lbl.mrow
|
||||
self.grid = Grid(wri, row, col, colwidth, rows, cols, justify=Label.CENTRE)
|
||||
for n, day in enumerate(DateCal.days):
|
||||
self.grid[[0, n]] = day[:3]
|
||||
self.grid[0, n] = day[:3]
|
||||
|
||||
row = self.grid.mrow + 4
|
||||
ht = 30
|
||||
|
|
|
@ -35,7 +35,7 @@ class Grid(Widget):
|
|||
c = col
|
||||
|
||||
def _idx(self, n):
|
||||
if isinstance(n, tuple) or isinstance(n, list):
|
||||
if isinstance(n, tuple) or isinstance(n, list): # list allows old syntax l[[r, c]]
|
||||
if n[0] >= self.nrows:
|
||||
raise ValueError("Grid row index too large")
|
||||
if n[1] >= self.ncols:
|
||||
|
@ -47,13 +47,14 @@ class Grid(Widget):
|
|||
raise ValueError("Grid cell index too large")
|
||||
return idx
|
||||
|
||||
def __getitem__(self, n): # Return the Label instance
|
||||
return self.cells[self._idx(n)]
|
||||
def __getitem__(self, *args): # Return the Label instance
|
||||
return self.cells[self._idx(args[0])]
|
||||
|
||||
# allow grid[[r, c]] = "foo" or kwargs for Label:
|
||||
# grid[[r, c]] = {"text": str(n), "fgcolor" : RED}
|
||||
def __setitem__(self, n, x):
|
||||
v = self.cells[self._idx(n)].value
|
||||
def __setitem__(self, *args):
|
||||
v = self.cells[self._idx(args[0])].value
|
||||
x = args[1]
|
||||
_ = v(**x) if isinstance(x, dict) else v(x)
|
||||
|
||||
def show(self):
|
||||
|
|
Ładowanie…
Reference in New Issue