grid widget: Allow proper 2D array syntax.

pull/56/head
peterhinch 2023-05-26 09:48:58 +01:00
rodzic 0ce46a7426
commit f6365fc76d
4 zmienionych plików z 18 dodań i 17 usunięć

Wyświetl plik

@ -7,6 +7,7 @@
# UC8176 manual https://www.waveshare.com/w/upload/8/88/UC8176.pdf # UC8176 manual https://www.waveshare.com/w/upload/8/88/UC8176.pdf
# Waveshare's copy of this driver. # Waveshare's copy of this driver.
# https://github.com/waveshare/Pico_ePaper_Code/blob/main/pythonNanoGui/drivers/ePaper4in2.py # https://github.com/waveshare/Pico_ePaper_Code/blob/main/pythonNanoGui/drivers/ePaper4in2.py
# https://github.com/waveshare/Pico_ePaper_Code/blob/main/python/Pico-ePaper-4.2.py
# ***************************************************************************** # *****************************************************************************
# * | File : Pico_ePaper-3.7.py # * | File : Pico_ePaper-3.7.py

Wyświetl plik

@ -40,8 +40,7 @@ specified in pixels with the column width being the specified width +4 to
allow for borders. The dimensions of the widget including borders are thus: allow for borders. The dimensions of the widget including borders are thus:
height = no. of rows * (font height + 4) height = no. of rows * (font height + 4)
width = sum(column width + 4) width = sum(column width + 4)
Cells may be addressed as a 1-dimensional list or by a `[row, col]` 2-list or Cells may be addressed as a 1 or 2-dimensional array.
2-tuple.
Constructor args: Constructor args:
1. `writer` The `Writer` instance (font and screen) to use. 1. `writer` The `Writer` instance (font and screen) to use.
@ -94,22 +93,22 @@ grid.show() # Draw grid lines
# Populate grid # Populate grid
col = 0 col = 0
for y, txt in enumerate("ABCDE"): for y, txt in enumerate("ABCDE"):
grid[[y + 1, col]] = txt grid[y + 1, col] = txt
row = 0 row = 0
for col in range(1, cols): for col in range(1, cols):
grid[[row, col]] = str(col) grid[row, col] = str(col)
grid[20] = "" # Clear cell 20 by setting its value to "" grid[20] = "" # Clear cell 20 by setting its value to ""
grid[[2, 5]] = str(42) # Note syntax grid[2, 5] = str(42) # 2d array syntax
# Dynamic formatting # Dynamic formatting
def txt(text): def txt(text):
return {"text": text} return {"text": text}
redfg = {"fgcolor": RED} redfg = {"fgcolor": RED}
grid[[3, 7]] = redfg | txt(str(99)) # Specify color as well as text grid[3, 7] = redfg | txt(str(99)) # Specify color as well as text
invla = {"invert": True, "align": ALIGN_LEFT} invla = {"invert": True, "align": ALIGN_LEFT}
grid[[2, 1]] = invla | txt("x") # Invert using invert flag grid[2, 1] = invla | txt("x") # Invert using invert flag
bkongn = {"fgcolor": BLACK, "bgcolor": GREEN, "align": ALIGN_LEFT} # Invert by swapping bg and fg bkongn = {"fgcolor": BLACK, "bgcolor": GREEN, "align": ALIGN_LEFT} # Invert by swapping bg and fg
grid[[3, 1]] = bkongn | txt("a") grid[3, 1] = bkongn | txt("a")
grid[[4,2]] = {"fgcolor": BLUE} | txt("go") grid[4,2] = {"fgcolor": BLUE} | txt("go")
refresh(ssd) refresh(ssd)
``` ```
## Calendar ## Calendar

Wyświetl plik

@ -30,7 +30,7 @@ class Calendar:
self.grid = Grid(wri, row, col, colwidth, rows, cols, **kwargs) self.grid = Grid(wri, row, col, colwidth, rows, cols, **kwargs)
self.grid.show() # Draw grid lines self.grid.show() # Draw grid lines
for n, day in enumerate(DateCal.days): # Populate day names for n, day in enumerate(DateCal.days): # Populate day names
self.grid[[0, n]] = day[:3] self.grid[0, n] = day[:3]
self.show() self.show()
def show(self): def show(self):

Wyświetl plik

@ -34,7 +34,7 @@ class Grid(DObject):
c = col c = col
def _idx(self, n): 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: if n[0] >= self.nrows:
raise ValueError("Grid row index too large") raise ValueError("Grid row index too large")
if n[1] >= self.ncols: if n[1] >= self.ncols:
@ -46,13 +46,14 @@ class Grid(DObject):
raise ValueError("Grid cell index too large") raise ValueError("Grid cell index too large")
return idx return idx
def __getitem__(self, n): # Return the Label instance def __getitem__(self, *args): # Return the Label instance
return self.cells[self._idx(n)] return self.cells[self._idx(args[0])]
# 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}
def __setitem__(self, n, x): def __setitem__(self, *args):
v = self.cells[self._idx(n)].value v = self.cells[self._idx(args[0])].value
x = args[1]
_ = v(**x) if isinstance(x, dict) else v(x) _ = v(**x) if isinstance(x, dict) else v(x)
def show(self): def show(self):