Skip to content

Commit ca714e5

Browse files
authored
Merge pull request #46 from FoamyGuy/get_cell
adding get_cell function to GridLayout
2 parents cb263f1 + 772aabd commit ca714e5

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

adafruit_displayio_layout/layouts/grid_layout.py

+18
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,21 @@ def add_content(self, cell_content, grid_position, cell_size):
121121
}
122122
self._cell_content_list.append(sub_view_obj)
123123
self._layout_cells()
124+
125+
def get_cell(self, cell_coordinates):
126+
"""
127+
Return a cells content based on the cell_coordinates. Raises
128+
KeyError if coordinates were not found in the GridLayout.
129+
130+
:param tuple cell_coordinates: the coordinates to lookup in the grid
131+
:return: the displayio content object at those coordinates
132+
"""
133+
for index, cell in enumerate(self._cell_content_list):
134+
if cell["grid_position"] == cell_coordinates:
135+
return self._cell_content_list[index]["content"]
136+
137+
raise KeyError(
138+
"GridLayout does not contain cell at coordinates {}".format(
139+
cell_coordinates
140+
)
141+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# SPDX-FileCopyrightText: 2021 Tim C, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
Make green and purple rectangles and then update the color
6+
and text values of the labels using the get_cell() function.
7+
"""
8+
import board
9+
import displayio
10+
import terminalio
11+
from adafruit_display_text import label
12+
from adafruit_displayio_layout.layouts.grid_layout import GridLayout
13+
14+
# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.)
15+
# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
16+
# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
17+
display = board.DISPLAY
18+
19+
# Make the display context
20+
main_group = displayio.Group()
21+
display.show(main_group)
22+
23+
layout = GridLayout(
24+
x=10,
25+
y=10,
26+
width=200,
27+
height=100,
28+
grid_size=(2, 2),
29+
cell_padding=8,
30+
)
31+
_labels = []
32+
33+
_labels.append(
34+
label.Label(
35+
terminalio.FONT, scale=2, x=0, y=0, text="Hello", background_color=0x770077
36+
)
37+
)
38+
layout.add_content(_labels[0], grid_position=(0, 0), cell_size=(1, 1))
39+
_labels.append(
40+
label.Label(
41+
terminalio.FONT, scale=2, x=0, y=0, text="World", background_color=0x007700
42+
)
43+
)
44+
layout.add_content(_labels[1], grid_position=(1, 0), cell_size=(1, 1))
45+
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="Hello"))
46+
layout.add_content(_labels[2], grid_position=(0, 1), cell_size=(1, 1))
47+
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="Grid"))
48+
layout.add_content(_labels[3], grid_position=(1, 1), cell_size=(1, 1))
49+
50+
main_group.append(layout)
51+
52+
layout.get_cell((0, 0)).text = "Happy"
53+
layout.get_cell((1, 0)).text = "Circuit"
54+
55+
layout.get_cell((0, 1)).text = "Python"
56+
layout.get_cell((1, 1)).text = "Day"
57+
58+
layout.get_cell((0, 1)).background_color = 0x007700
59+
layout.get_cell((1, 1)).background_color = 0x770077
60+
61+
while True:
62+
pass

0 commit comments

Comments
 (0)