Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions adafruit_displayio_layout/layouts/grid_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self, x, y, width, height, grid_size, cell_padding):
self.grid_size = grid_size
self.cell_padding = cell_padding
self._cell_content_list = []
self._cell_dictionary = {}

def _layout_cells(self):

Expand Down Expand Up @@ -120,4 +121,24 @@ def add_content(self, cell_content, grid_position, cell_size):
"cell_size": cell_size,
}
self._cell_content_list.append(sub_view_obj)
self._cell_dictionary[grid_position] = len(self._cell_content_list) - 1
self._layout_cells()

def get_cell(self, cell_coordinates):
"""
Return a cells content based on the cell_coordinates. Raises
KeyError if coordinates were not found in the GridLayout.

:param tuple cell_coordinates: the coordinates to lookup in the grid
:return: the displayio content object at those coordinates
"""
if cell_coordinates in self._cell_dictionary:
return self._cell_content_list[self._cell_dictionary[cell_coordinates]][
"content"
]

raise KeyError(
"GridLayout does not contain cell at coordinates {}".format(
cell_coordinates
)
)
62 changes: 62 additions & 0 deletions examples/displayio_layout_grid_layout_get_cell_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# SPDX-FileCopyrightText: 2021 Tim C, written for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
Make green and purple rectangles and then update the color
and text values of the labels using the get_cell() function.
"""
import board
import displayio
import terminalio
from adafruit_display_text import label
from adafruit_displayio_layout.layouts.grid_layout import GridLayout

# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.)
# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
display = board.DISPLAY

# Make the display context
main_group = displayio.Group()
display.show(main_group)

layout = GridLayout(
x=10,
y=10,
width=200,
height=100,
grid_size=(2, 2),
cell_padding=8,
)
_labels = []

_labels.append(
label.Label(
terminalio.FONT, scale=2, x=0, y=0, text="Hello", background_color=0x770077
)
)
layout.add_content(_labels[0], grid_position=(0, 0), cell_size=(1, 1))
_labels.append(
label.Label(
terminalio.FONT, scale=2, x=0, y=0, text="World", background_color=0x007700
)
)
layout.add_content(_labels[1], grid_position=(1, 0), cell_size=(1, 1))
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="Hello"))
layout.add_content(_labels[2], grid_position=(0, 1), cell_size=(1, 1))
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="Grid"))
layout.add_content(_labels[3], grid_position=(1, 1), cell_size=(1, 1))

main_group.append(layout)

layout.get_cell((0, 0)).text = "Happy"
layout.get_cell((1, 0)).text = "Circuit"

layout.get_cell((0, 1)).text = "Python"
layout.get_cell((1, 1)).text = "Day"

layout.get_cell((0, 1)).background_color = 0x007700
layout.get_cell((1, 1)).background_color = 0x770077

while True:
pass