Skip to content

Commit 801bdcb

Browse files
committed
tab layout touch example
1 parent bdb03c9 commit 801bdcb

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# SPDX-FileCopyrightText: 2022 Tim C
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
Make a TabLayout change tabs with the touchscreen
6+
"""
7+
import displayio
8+
import board
9+
import terminalio
10+
import adafruit_touchscreen
11+
from adafruit_display_text.bitmap_label import Label
12+
from adafruit_display_shapes.rect import Rect
13+
from adafruit_display_shapes.circle import Circle
14+
from adafruit_display_shapes.triangle import Triangle
15+
from adafruit_displayio_layout.layouts.tab_layout import TabLayout
16+
17+
# built-in display
18+
display = board.DISPLAY
19+
20+
# ------------ Touchscreen setup --------------- #
21+
# See: https://learn.adafruit.com/making-a-pyportal-user-interface-displayio/display
22+
display = board.DISPLAY # create the display object
23+
24+
screen_width = display.width
25+
screen_height = display.height
26+
ts = adafruit_touchscreen.Touchscreen(
27+
board.TOUCH_XL,
28+
board.TOUCH_XR,
29+
board.TOUCH_YD,
30+
board.TOUCH_YU,
31+
calibration=((5200, 59000), (5800, 57000)),
32+
size=(screen_width, screen_height),
33+
)
34+
35+
# create and show main_group
36+
main_group = displayio.Group()
37+
display.show(main_group)
38+
39+
font = terminalio.FONT
40+
41+
# create the page layout
42+
test_page_layout = TabLayout(
43+
x=0,
44+
y=0,
45+
display=board.DISPLAY,
46+
tab_text_scale=2,
47+
custom_font=font,
48+
inactive_tab_spritesheet="bmps/inactive_tab_sprite.bmp",
49+
active_tab_spritesheet="bmps/active_tab_sprite.bmp",
50+
active_tab_text_color=0x00AA59,
51+
inactive_tab_text_color=0xEEEEEE,
52+
inactive_tab_transparent_indexes=(0, 1),
53+
active_tab_transparent_indexes=(0, 1),
54+
tab_count=4,
55+
)
56+
57+
# make page content Groups
58+
page_1_group = displayio.Group()
59+
page_2_group = displayio.Group()
60+
page_3_group = displayio.Group()
61+
page_4_group = displayio.Group()
62+
63+
# labels
64+
page_1_lbl = Label(
65+
font=terminalio.FONT,
66+
scale=2,
67+
text="This is the first page!",
68+
anchor_point=(0, 0),
69+
anchored_position=(10, 10),
70+
)
71+
page_2_lbl = Label(
72+
font=terminalio.FONT,
73+
scale=2,
74+
text="This page is the\nsecond page!",
75+
anchor_point=(0, 0),
76+
anchored_position=(10, 10),
77+
)
78+
page_3_lbl = Label(
79+
font=terminalio.FONT,
80+
scale=2,
81+
text="The third page is fun!",
82+
anchor_point=(0, 0),
83+
anchored_position=(10, 10),
84+
)
85+
86+
page_4_lbl = Label(
87+
font=terminalio.FONT,
88+
scale=2,
89+
text="The fourth page\nis where it's at",
90+
anchor_point=(0, 0),
91+
anchored_position=(10, 10),
92+
)
93+
94+
# shapes
95+
square = Rect(x=20, y=70, width=40, height=40, fill=0x00DD00)
96+
circle = Circle(50, 120, r=30, fill=0xDD00DD)
97+
triangle = Triangle(50, 0, 100, 50, 0, 50, fill=0xDDDD00)
98+
rectangle = Rect(x=80, y=80, width=100, height=50, fill=0x0000DD)
99+
100+
triangle.x = 80
101+
triangle.y = 70
102+
103+
# add everything to their page groups
104+
page_1_group.append(square)
105+
page_1_group.append(page_1_lbl)
106+
page_2_group.append(page_2_lbl)
107+
page_2_group.append(circle)
108+
page_3_group.append(page_3_lbl)
109+
page_3_group.append(triangle)
110+
page_4_group.append(page_4_lbl)
111+
page_4_group.append(rectangle)
112+
113+
# add the pages to the layout, supply your own page names
114+
test_page_layout.add_content(page_1_group, "One")
115+
test_page_layout.add_content(page_2_group, "Two")
116+
test_page_layout.add_content(page_3_group, "Thr")
117+
test_page_layout.add_content(page_4_group, "For")
118+
119+
# add it to the group that is showing on the display
120+
main_group.append(test_page_layout)
121+
122+
123+
# add something new after the TabLayout was already created
124+
another_text = Label(
125+
terminalio.FONT,
126+
text="And another thing!",
127+
scale=2,
128+
color=0x00FF00,
129+
anchor_point=(0, 0),
130+
anchored_position=(100, 100),
131+
)
132+
test_page_layout.showing_page_content.append(another_text)
133+
134+
while True:
135+
touch = ts.touch_point
136+
if touch:
137+
test_page_layout.handle_touch_events(touch)

0 commit comments

Comments
 (0)