Simple Test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import board
5import terminalio
6from adafruit_display_text import label
7
8
9text = "Hello world"
10text_area = label.Label(terminalio.FONT, text=text)
11text_area.x = 10
12text_area.y = 10
13board.DISPLAY.root_group = text_area
14while True:
15 pass
Bitmap_label Simple Test¶
Simple test using bitmap_label to display text
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import board
5import terminalio
6from adafruit_display_text import bitmap_label
7
8
9text = "Hello world"
10text_area = bitmap_label.Label(terminalio.FONT, text=text)
11text_area.x = 10
12text_area.y = 10
13board.DISPLAY.root_group = text_area
14while True:
15 pass
ScrollingLabel Simple Test¶
Simple test using scrolling_label to display text
1# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import board
5import terminalio
6from adafruit_display_text.scrolling_label import ScrollingLabel
7
8
9text = "Hello world CircuitPython scrolling label"
10my_scrolling_label = ScrollingLabel(
11 terminalio.FONT, text=text, max_characters=20, animate_time=0.3
12)
13my_scrolling_label.x = 10
14my_scrolling_label.y = 10
15board.DISPLAY.root_group = my_scrolling_label
16while True:
17 my_scrolling_label.update()
Label vs Bitmap_label Comparison¶
Example to compare Label and Bitmap_Label characteristics
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# Sample for comparing label and bitmap_label positioning with Builtin or loaded BDF fonts
5
6# pylint: disable=no-member
7
8import gc
9import board
10import displayio
11import terminalio
12from adafruit_bitmap_font import bitmap_font
13
14from adafruit_display_text import bitmap_label
15from adafruit_display_text import label
16
17# pylint: disable=no-member
18
19
20##########
21# Use this Boolean variables to select which font style to use
22##########
23use_builtinfont = False # Set True to use the terminalio.FONT BuiltinFont,
24fontToUse = terminalio.FONT
25# Set False to use a BDF loaded font, see "fontFiles" below
26##########
27
28if not use_builtinfont:
29 # load the fonts
30 print("loading font...")
31
32 fontList = []
33
34 # Load some proportional fonts
35 fontFile = "fonts/LeagueSpartan-Bold-16.bdf"
36 fontToUse = bitmap_font.load_font(fontFile)
37
38# Set scaling factor for display text
39my_scale = 1
40
41# Setup the SPI display
42if "DISPLAY" in dir(board):
43 # use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.)
44 # see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
45 # https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
46 display = board.DISPLAY
47
48else:
49 # Setup the LCD display with driver
50 # You may need to change this to match the display driver for the chipset
51 # used on your display
52 from adafruit_ili9341 import ILI9341
53
54 displayio.release_displays()
55
56 # setup the SPI bus
57 spi = board.SPI()
58 tft_cs = board.D9 # arbitrary, pin not used
59 tft_dc = board.D10
60 tft_backlight = board.D12
61 tft_reset = board.D11
62
63 while not spi.try_lock():
64 spi.configure(baudrate=32000000)
65 spi.unlock()
66
67 display_bus = displayio.FourWire(
68 spi,
69 command=tft_dc,
70 chip_select=tft_cs,
71 reset=tft_reset,
72 baudrate=32000000,
73 polarity=1,
74 phase=1,
75 )
76
77 # Number of pixels in the display
78 DISPLAY_WIDTH = 320
79 DISPLAY_HEIGHT = 240
80
81 # create the display
82 display = ILI9341(
83 display_bus,
84 width=DISPLAY_WIDTH,
85 height=DISPLAY_HEIGHT,
86 rotation=180, # The rotation can be adjusted to match your configuration.
87 auto_refresh=True,
88 native_frames_per_second=90,
89 )
90
91 # reset the display to show nothing.
92 display.root_group = None
93
94print("Display is started")
95
96preload_glyphs = (
97 True # set this to True if you want to preload the font glyphs into memory
98)
99# preloading the glyphs will help speed up the rendering of text but will use more RAM
100
101if preload_glyphs and not use_builtinfont:
102 # identify the glyphs to load into memory -> increases rendering speed
103 glyphs = (
104 b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/-_,.:?!'\n "
105 )
106
107 print("loading glyphs...")
108 fontToUse.load_glyphs(glyphs)
109
110 print("Glyphs are loaded.")
111
112print("Fonts completed loading.")
113
114# create group
115
116long_string = "The purple snake\nbrings python fun\nto everyone."
117label2_padding = 10
118
119#####
120# Create the "bitmap_label.py" versions of the text labels.
121
122gc.collect()
123bitmap_label_start = gc.mem_free()
124
125bmap_label1 = bitmap_label.Label(
126 font=fontToUse,
127 text="bitmap_label",
128 color=0xFFFFFF,
129 background_color=0xFF0000,
130 padding_bottom=0,
131 padding_left=0,
132 padding_right=0,
133 padding_top=0,
134 background_tight=True,
135 line_spacing=1.25,
136 scale=my_scale,
137 anchor_point=(0.0, 0),
138 anchored_position=(10, 60),
139)
140
141bmap_label2 = bitmap_label.Label(
142 font=fontToUse,
143 text=long_string,
144 color=0x000000,
145 background_color=0xFFFF00,
146 padding_bottom=label2_padding,
147 padding_left=0,
148 padding_right=0,
149 padding_top=label2_padding,
150 background_tight=False,
151 line_spacing=1.25,
152 scale=my_scale,
153 anchor_point=(0.0, 0),
154 anchored_position=(10, 120),
155)
156
157gc.collect()
158bitmap_label_end = gc.mem_free()
159
160print("bitmap_label used: {} memory".format(bitmap_label_start - bitmap_label_end))
161
162bmap_group = displayio.Group() # Create a group for displaying
163bmap_group.append(bmap_label1)
164bmap_group.append(bmap_label2)
165
166
167#####
168# Create the "label.py" versions of the text labels.
169
170gc.collect()
171label_start = gc.mem_free()
172
173label1 = label.Label(
174 font=fontToUse,
175 text="label",
176 color=0xFFFFFF,
177 background_color=0xFF0000,
178 padding_bottom=0,
179 padding_left=0,
180 padding_right=0,
181 padding_top=0,
182 background_tight=True,
183 line_spacing=1.25,
184 scale=my_scale,
185 anchor_point=(1.0, 0),
186 anchored_position=(display.width - 10, 60),
187)
188
189label2 = label.Label(
190 font=fontToUse,
191 text=long_string,
192 color=0x000000,
193 background_color=0xFFFF00,
194 padding_bottom=label2_padding,
195 padding_left=0,
196 padding_right=0,
197 padding_top=label2_padding,
198 background_tight=False,
199 line_spacing=1.25,
200 scale=my_scale,
201 anchor_point=(1.0, 0),
202 anchored_position=(display.width - 10, 120),
203)
204
205gc.collect()
206label_end = gc.mem_free()
207
208print("label used: {} memory".format(label_start - label_end))
209label_group = displayio.Group() # Create a group for displaying
210label_group.append(label1)
211label_group.append(label2)
212
213
214print("***")
215
216main_group = displayio.Group()
217main_group.append(label_group)
218main_group.append(bmap_group)
219
220display.auto_refresh = True
221
222display.root_group = main_group
223while True:
224 pass
Background Color Example¶
Show the text backgrounds features
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4"""
5This example shows the use color and background_color
6"""
7import time
8import board
9import terminalio
10from adafruit_display_text import label
11
12text = " Color Background Hello world"
13text_area = label.Label(
14 terminalio.FONT, text=text, color=0x0000FF, background_color=0xFFAA00
15)
16text_area.x = 10
17text_area.y = 10
18
19print("background color is {:06x}".format(text_area.background_color))
20
21board.DISPLAY.root_group = text_area
22
23time.sleep(2)
24text_area.background_color = 0xFF0000
25print("background color is {:06x}".format(text_area.background_color))
26time.sleep(2)
27text_area.background_color = None
28print("background color is {}".format(text_area.background_color))
29while True:
30 pass
Text Padding Example¶
Show the text padding features in all directions
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4"""
5This example shows the use color and background_color
6"""
7import time
8import board
9import displayio
10
11from adafruit_bitmap_font import bitmap_font
12from adafruit_display_text import label
13
14
15# Setup the SPI display
16if "DISPLAY" in dir(board):
17 # use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.)
18 # see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
19 # https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
20 display = board.DISPLAY
21
22else:
23 print("Starting external display") # goes to serial only
24 # Setup the LCD display with driver
25 # You may need to change this to match the display driver for the chipset
26 # used on your display
27 from adafruit_ili9341 import ILI9341
28
29 # from adafruit_st7789 import ST7789
30
31 displayio.release_displays()
32
33 # setup the SPI bus
34 spi = board.SPI()
35 tft_cs = board.D9 # arbitrary, pin not used
36 tft_dc = board.D10
37 tft_backlight = board.D12
38 tft_reset = board.D11
39
40 while not spi.try_lock():
41 spi.configure(baudrate=32000000)
42 spi.unlock()
43
44 display_bus = displayio.FourWire(
45 spi,
46 command=tft_dc,
47 chip_select=tft_cs,
48 reset=tft_reset,
49 baudrate=32000000,
50 polarity=1,
51 phase=1,
52 )
53
54 # Number of pixels in the display
55 DISPLAY_WIDTH = 320
56 DISPLAY_HEIGHT = 240
57
58 # display = ST7789(display_bus, width=240, height=240, rotation=0, rowstart=80, colstart=0)
59
60 # create the display
61 display = ILI9341(
62 display_bus,
63 width=DISPLAY_WIDTH,
64 height=DISPLAY_HEIGHT,
65 rotation=180, # The rotation can be adjusted to match your configuration.
66 auto_refresh=True,
67 native_frames_per_second=90,
68 )
69
70display.root_group = None
71
72# font=terminalio.FONT # this is the Builtin fixed dimension font
73
74font = bitmap_font.load_font("fonts/LeagueSpartan-Bold-16.bdf")
75
76text = []
77text.append("none") # no ascenders or descenders
78text.append("pop quops") # only descenders
79text.append("MONSTERs are tall") # only ascenders
80text.append("MONSTERs ate pop quops") # both ascenders and descenders
81text.append("MONSTER quops\nnewline quops") # with newline
82
83display.auto_refresh = True
84myGroup = displayio.Group()
85display.root_group = myGroup
86
87text_area = []
88myPadding = 4
89
90for i, thisText in enumerate(text):
91 text_area.append(
92 label.Label(
93 font,
94 text=thisText,
95 color=0xFFFFFF,
96 background_color=None,
97 background_tight=False,
98 padding_top=myPadding,
99 padding_bottom=myPadding,
100 padding_left=myPadding,
101 padding_right=myPadding,
102 )
103 )
104
105 this_x = 10
106 this_y = 10 + i * 40
107 text_area[i].x = 10
108 text_area[i].y = 3 + i * 50
109 text_area[i].anchor_point = (0, 0)
110 text_area[i].anchored_position = (this_x, this_y)
111 myGroup.append(text_area[i])
112
113print("background color is {}".format(text_area[0].background_color))
114
115
116while True:
117 time.sleep(2)
118 text_area[0].text = "text" # change some text in an existing text box
119 # Note: changed text must fit within existing number of characters
120 # when the Label was created
121
122 for area in text_area:
123 area.background_color = 0xFF0000
124 print("background color is {:06x}".format(text_area[0].background_color))
125 time.sleep(2)
126 for area in text_area:
127 area.background_color = 0x000088
128 print("background color is {:06x}".format(text_area[0].background_color))
129 time.sleep(2)
130 for area in text_area:
131 area.background_color = 0x00FF00
132 print("background color is {:06x}".format(text_area[0].background_color))
133 time.sleep(2)
134 for area in text_area:
135 area.background_color = 0xFF0000
136 print("background color is {:06x}".format(text_area[0].background_color))
137 time.sleep(2)
138 for area in text_area:
139 area.background_color = None
140 print("background color is {}".format(text_area[0].background_color))
Anchored Position¶
Anchored position use illustration
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4"""
5This examples shows the use of anchor_point and anchored_position.
6"""
7import board
8import terminalio
9import displayio
10from adafruit_display_text import label
11
12DISPLAY_WIDTH = 320
13DISPLAY_HEIGHT = 240
14TEXT = "Hello"
15
16text_area_top_left = label.Label(terminalio.FONT, text=TEXT)
17text_area_top_left.anchor_point = (0.0, 0.0)
18text_area_top_left.anchored_position = (0, 0)
19
20text_area_top_middle = label.Label(terminalio.FONT, text=TEXT)
21text_area_top_middle.anchor_point = (0.5, 0.0)
22text_area_top_middle.anchored_position = (DISPLAY_WIDTH / 2, 0)
23
24text_area_top_right = label.Label(terminalio.FONT, text=TEXT)
25text_area_top_right.anchor_point = (1.0, 0.0)
26text_area_top_right.anchored_position = (DISPLAY_WIDTH, 0)
27
28text_area_middle_left = label.Label(terminalio.FONT, text=TEXT)
29text_area_middle_left.anchor_point = (0.0, 0.5)
30text_area_middle_left.anchored_position = (0, DISPLAY_HEIGHT / 2)
31
32text_area_middle_middle = label.Label(terminalio.FONT, text=TEXT)
33text_area_middle_middle.anchor_point = (0.5, 0.5)
34text_area_middle_middle.anchored_position = (DISPLAY_WIDTH / 2, DISPLAY_HEIGHT / 2)
35
36text_area_middle_right = label.Label(terminalio.FONT, text=TEXT)
37text_area_middle_right.anchor_point = (1.0, 0.5)
38text_area_middle_right.anchored_position = (DISPLAY_WIDTH, DISPLAY_HEIGHT / 2)
39
40text_area_bottom_left = label.Label(terminalio.FONT, text=TEXT)
41text_area_bottom_left.anchor_point = (0.0, 1.0)
42text_area_bottom_left.anchored_position = (0, DISPLAY_HEIGHT)
43
44text_area_bottom_middle = label.Label(terminalio.FONT, text=TEXT)
45text_area_bottom_middle.anchor_point = (0.5, 1.0)
46text_area_bottom_middle.anchored_position = (DISPLAY_WIDTH / 2, DISPLAY_HEIGHT)
47
48text_area_bottom_right = label.Label(terminalio.FONT, text=TEXT)
49text_area_bottom_right.anchor_point = (1.0, 1.0)
50text_area_bottom_right.anchored_position = (DISPLAY_WIDTH, DISPLAY_HEIGHT)
51
52text_group = displayio.Group()
53text_group.append(text_area_top_middle)
54text_group.append(text_area_top_left)
55text_group.append(text_area_top_right)
56text_group.append(text_area_middle_middle)
57text_group.append(text_area_middle_left)
58text_group.append(text_area_middle_right)
59text_group.append(text_area_bottom_middle)
60text_group.append(text_area_bottom_left)
61text_group.append(text_area_bottom_right)
62
63board.DISPLAY.root_group = text_group
64
65while True:
66 pass
Textarea Boundingbox¶
Boundingbox demonstration
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import os
5import board
6import displayio
7from adafruit_bitmap_font import bitmap_font
8from adafruit_display_text.label import Label
9
10
11# the current working directory (where this file is)
12cwd = ("/" + __file__).rsplit("/", 1)[0]
13fonts = [
14 file
15 for file in os.listdir(cwd + "/fonts/")
16 if (file.endswith(".bdf") and not file.startswith("._"))
17]
18for i, filename in enumerate(fonts):
19 fonts[i] = cwd + "/fonts/" + filename
20print(fonts)
21
22##########################################################################
23THE_FONT = fonts[0]
24DISPLAY_STRING = "A multi-line-\nexample of\n font bounding!"
25WRAP_CHARS = 40
26
27##########################################################################
28# Make the display context
29splash = displayio.Group()
30board.DISPLAY.root_group = splash
31
32# Make a background color fill
33color_bitmap = displayio.Bitmap(320, 240, 1)
34color_palette = displayio.Palette(1)
35color_palette[0] = 0xFFFFFF
36bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
37splash.append(bg_sprite)
38
39# Load the font
40font = bitmap_font.load_font(THE_FONT)
41font.load_glyphs(DISPLAY_STRING.encode("utf-8"))
42
43print(DISPLAY_STRING)
44
45text = Label(font, text=DISPLAY_STRING)
46text.x = 20
47text.y = 100
48text.color = 0x0
49
50# Make a background color fill
51dims = text.bounding_box
52print(dims)
53textbg_bitmap = displayio.Bitmap(dims[2], dims[3], 1)
54textbg_palette = displayio.Palette(1)
55textbg_palette[0] = 0xFF0000
56textbg_sprite = displayio.TileGrid(
57 textbg_bitmap, pixel_shader=textbg_palette, x=text.x + dims[0], y=text.y + dims[1]
58)
59splash.append(textbg_sprite)
60splash.append(text)
61try:
62 board.DISPLAY.refresh(target_frames_per_second=60)
63except AttributeError:
64 board.DISPLAY.refresh_soon()
65 board.DISPLAY.wait_for_frame()
66
67
68while True:
69 pass
Align Baseline Example¶
Demonstrate how to align different labels to a common horizontal line
1# SPDX-FileCopyrightText: 2021 Jose David Montoya for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4"""
5This example shows the use of base_alignment parameter.
6"""
7
8import board
9import displayio
10from adafruit_bitmap_font import bitmap_font
11from adafruit_display_text import label
12
13
14display = board.DISPLAY
15
16# Font definition. You can choose any two fonts available in your system
17MEDIUM_FONT = bitmap_font.load_font("LeagueSpartan-Bold-16.bdf")
18BIG_FONT = bitmap_font.load_font("LibreBodoniv2002-Bold-27.bdf")
19
20TEXT_RIGHT = "MG"
21TEXT_LEFT = "32.47"
22
23main_group = displayio.Group()
24
25# Create labels
26# Base Alignment parameter False
27left_text = label.Label(
28 BIG_FONT,
29 text=TEXT_LEFT,
30 color=0x000000,
31 background_color=0x999999,
32 x=10,
33 y=50,
34 base_alignment=False,
35)
36main_group.append(left_text)
37
38right_text = label.Label(
39 MEDIUM_FONT,
40 text=TEXT_RIGHT,
41 color=0x000000,
42 background_color=0x999999,
43 x=90,
44 y=50,
45 base_alignment=False,
46)
47main_group.append(right_text)
48
49# Base Alignment parameter True
50left_text_aligned = label.Label(
51 BIG_FONT,
52 text=TEXT_LEFT,
53 color=0x000000,
54 background_color=0x999999,
55 x=10,
56 y=100,
57 base_alignment=True,
58)
59main_group.append(left_text_aligned)
60
61right_text_aligned = label.Label(
62 MEDIUM_FONT,
63 text=TEXT_RIGHT,
64 color=0x000000,
65 background_color=0x999999,
66 x=90,
67 y=100,
68 base_alignment=True,
69)
70
71main_group.append(right_text_aligned)
72display.root_group = main_group
73
74while True:
75 pass
Magtag Example¶
Uses the MAGTAG to display some text
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4"""
5Basic display_text.label example script
6adapted for use on MagTag.
7"""
8import time
9import board
10import displayio
11import terminalio
12from adafruit_display_text import label
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
17display = board.DISPLAY
18
19# wait until we can draw
20time.sleep(display.time_to_refresh)
21
22# main group to hold everything
23main_group = displayio.Group()
24
25# white background. Scaled to save RAM
26bg_bitmap = displayio.Bitmap(display.width // 8, display.height // 8, 1)
27bg_palette = displayio.Palette(1)
28bg_palette[0] = 0xFFFFFF
29bg_sprite = displayio.TileGrid(bg_bitmap, x=0, y=0, pixel_shader=bg_palette)
30bg_group = displayio.Group(scale=8)
31bg_group.append(bg_sprite)
32main_group.append(bg_group)
33
34# first example label
35TEXT = "Hello world"
36text_area = label.Label(
37 terminalio.FONT,
38 text=TEXT,
39 color=0xFFFFFF,
40 background_color=0x666666,
41 padding_top=1,
42 padding_bottom=3,
43 padding_right=4,
44 padding_left=4,
45)
46text_area.x = 10
47text_area.y = 14
48main_group.append(text_area)
49
50# second example label
51another_text = label.Label(
52 terminalio.FONT,
53 scale=2,
54 text="MagTag display_text\nexample",
55 color=0x000000,
56 background_color=0x999999,
57 padding_top=1,
58 padding_bottom=3,
59 padding_right=4,
60 padding_left=4,
61)
62# centered
63another_text.anchor_point = (0.5, 0.5)
64another_text.anchored_position = (display.width // 2, display.height // 2)
65main_group.append(another_text)
66
67# show the main group and refresh.
68display.root_group = main_group
69display.refresh()
70while True:
71 pass
MatrixPortal Example¶
Uses the MatrixPortal to display some text
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4"""
5This example shows how to create a display_text label and show it
6with a Matrix Portal
7
8Requires:
9adafruit_matrixportal - https://github.com/adafruit/Adafruit_CircuitPython_MatrixPortal
10
11Copy it from the current libraries bundle into the lib folder on your device.
12"""
13import terminalio
14from adafruit_matrixportal.matrix import Matrix
15from adafruit_display_text import label
16
17matrix = Matrix()
18display = matrix.display
19
20text = "Hello\nworld"
21text_area = label.Label(terminalio.FONT, text=text)
22text_area.x = 1
23text_area.y = 4
24display.root_group = text_area
25while True:
26 pass
PyPortal Example¶
Uses the Pyportal to display some text
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4"""
5This example show the use of the backlight as well as using labels to simulate
6a terminal using a font on the PyPortal
7"""
8
9import os
10import time
11import board
12import displayio
13
14from adafruit_bitmap_font import bitmap_font
15from adafruit_display_text.label import Label
16
17FONT_DIR = "/fonts/"
18fonts = list(
19 filter(lambda x: x.endswith("bdf") and not x.startswith("."), os.listdir(FONT_DIR))
20)
21fonts = [bitmap_font.load_font(FONT_DIR + x) for x in fonts]
22if len(fonts) == 0:
23 print("No fonts found in '{}'".format(FONT_DIR))
24
25print("fade up")
26# Fade up the backlight
27for b in range(100):
28 board.DISPLAY.brightness = b / 100
29 time.sleep(0.01) # default (0.01)
30
31demos = ["CircuitPython = Code + Community", "accents - üàêùéáçãÍóí", "others - αψ◌"]
32
33splash = displayio.Group()
34board.DISPLAY.root_group = splash
35max_y = 0
36y = 0
37for demo_text in demos:
38 for font in fonts:
39 if y >= board.DISPLAY.height:
40 y = 0
41 while len(splash):
42 splash.pop()
43 print("Font load {}".format(font.name))
44 area = Label(
45 font, text=demo_text, anchor_point=(0, 0), anchored_position=(0, y)
46 )
47 splash.append(area)
48
49 y += area.height
50
51 # Wait for the image to load.
52 try:
53 board.DISPLAY.refresh(target_frames_per_second=60)
54 except AttributeError:
55 board.DISPLAY.wait_for_frame()
56
57# Wait for 1 minute (60 seconds)
58time.sleep(60)
59
60# Fade down the backlight
61for b in range(100, -1, -1):
62 board.DISPLAY.brightness = b / 100
63 time.sleep(0.01) # default (0.01)
64
65print("fade down")
66
67time.sleep(10)
Wraptest Example¶
Illustrates the wraptest feature
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4"""
5This example illustrates how to use the wrap_text_to_lines
6helper function.
7"""
8import board
9import terminalio
10from adafruit_display_text import label, wrap_text_to_lines
11
12# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.)
13# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
14# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
15display = board.DISPLAY
16
17text = (
18 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, "
19 "sed do eiusmod tempor incididunt ut labore et dolore magna "
20 "aliqua. Ut enim ad minim veniam, quis nostrud exercitation "
21 "ullamco laboris nisi ut aliquip ex ea commodo consequat."
22)
23text = "\n".join(wrap_text_to_lines(text, 28))
24text_area = label.Label(terminalio.FONT, text=text)
25text_area.x = 10
26text_area.y = 10
27display.root_group = text_area
28while True:
29 pass
Wrap Pixel Test¶
Wrap Pixel Test
1# SPDX-FileCopyrightText: 2021 Tim C, written for Adafruit Industries
2#
3# SPDX-License-Identifier: MIT
4"""
5Test the wrap_text_to_pixels function. Try changing WRAP_WIDTH or text
6and observe the results. The red bar represents the full size of
7WRAP_WIDTH.
8"""
9
10import board
11import displayio
12import terminalio
13from adafruit_display_text import label, wrap_text_to_pixels
14
15WRAP_WIDTH = 140
16text = (
17 "CircuitPython is a programming language designed to simplify experimenting "
18 "and learning to code on low-cost microcontroller boards. "
19)
20
21# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.)
22# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
23# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
24display = board.DISPLAY
25
26# Make the display context
27main_group = displayio.Group()
28display.root_group = main_group
29
30font = terminalio.FONT
31
32print(text)
33print(display.width)
34
35text_area = label.Label(
36 font,
37 text="\n".join(wrap_text_to_pixels(text, WRAP_WIDTH, font)),
38 background_color=0x0000DD,
39)
40
41text_area.anchor_point = (0, 0)
42text_area.anchored_position = (0, 0)
43
44main_group.append(text_area)
45
46# Create a bitmap with two colors
47size_checker = displayio.Bitmap(WRAP_WIDTH, 10, 2)
48# Create a two color palette
49palette = displayio.Palette(2)
50palette[0] = 0x0000DD
51palette[1] = 0xDD0000
52
53# Create a TileGrid using the Bitmap and Palette
54tile_grid = displayio.TileGrid(size_checker, pixel_shader=palette)
55
56tile_grid.y = text_area.bounding_box[1] + text_area.bounding_box[3] + 10
57
58size_checker.fill(1)
59
60main_group.append(tile_grid)
61
62while True:
63 pass
Library Features Example¶
This examples shows the label
and bitmap_label
capabilities and features
1# SPDX-FileCopyrightText: 2021 Jose David M.
2#
3# SPDX-License-Identifier: MIT
4#############################
5"""
6This is an advanced demonstration of the display_text library capabilities
7"""
8
9import time
10import board
11import displayio
12import terminalio
13import fontio
14from adafruit_bitmap_font import bitmap_font
15from adafruit_display_text import label, bitmap_label
16
17display = board.DISPLAY
18main_group = displayio.Group()
19MEDIUM_FONT = bitmap_font.load_font("fonts/LeagueSpartan-Bold-16.bdf")
20BIG_FONT = bitmap_font.load_font("fonts/LibreBodoniv2002-Bold-27.bdf")
21TIME_PAUSE = 2
22
23bitmap = displayio.Bitmap(4, display.width, 2)
24palette = displayio.Palette(2)
25palette[0] = 0x004400
26palette[1] = 0x00FFFF
27horizontal_line = displayio.TileGrid(bitmap, pixel_shader=palette, x=155, y=0)
28main_group.append(horizontal_line)
29
30bitmap = displayio.Bitmap(display.width, 4, 2)
31vertical_line = displayio.TileGrid(bitmap, pixel_shader=palette, x=0, y=110)
32main_group.append(vertical_line)
33
34# Tests
35text_area = label.Label(terminalio.FONT, text="Circuit Python")
36main_group.append(text_area)
37display.root_group = main_group
38time.sleep(TIME_PAUSE)
39
40# Testing position setter
41text_area.x = 10
42text_area.y = 10
43display.root_group = main_group
44time.sleep(TIME_PAUSE)
45
46# Testing creating label with initial position
47text_area.text = "Testing initiating without text"
48try:
49 text_middle = label.Label(terminalio.FONT)
50except SyntaxError:
51 print("Fail setting-up label without text")
52 warning_text = label.Label(
53 BIG_FONT,
54 text="Test Fail",
55 x=display.width // 2,
56 y=display.height // 4,
57 background_color=0x004499,
58 )
59 main_group.append(warning_text)
60display.root_group = main_group
61time.sleep(TIME_PAUSE)
62
63text_area.text = "Testing Position"
64text_middle = label.Label(
65 terminalio.FONT, text="Circuit", x=display.width // 2, y=display.height // 2
66)
67main_group.append(text_middle)
68display.root_group = main_group
69time.sleep(TIME_PAUSE)
70
71# Testing Text Setter
72text_area.text = "Testing Changing Text"
73text_middle.text = "Python"
74display.root_group = main_group
75time.sleep(TIME_PAUSE)
76
77# Testing a and y getter and setter
78text_area.text = "Testing Changing Position"
79text_middle.x = text_middle.x - 50
80text_middle.y = text_middle.y - 50
81display.root_group = main_group
82time.sleep(TIME_PAUSE)
83
84# Testing font Getter and setter
85text_area.text = "Testing Changing FONT"
86if isinstance(text_middle.font, fontio.BuiltinFont):
87 text_middle.font = MEDIUM_FONT
88display.root_group = main_group
89time.sleep(TIME_PAUSE)
90
91# Once this working we create another label with all the initial specs
92main_group.pop()
93
94# Testing Color
95text_area.text = "Testing Color"
96text_initial_specs = label.Label(
97 MEDIUM_FONT,
98 text="Circuit Python",
99 x=display.width // 2,
100 y=display.height // 2,
101)
102main_group.append(text_initial_specs)
103display.root_group = main_group
104time.sleep(TIME_PAUSE)
105
106text_initial_specs.color = 0x004400
107display.root_group = main_group
108time.sleep(TIME_PAUSE)
109main_group.pop()
110
111# Testing Background Color
112text_area.text = "Testing Background Color"
113text_initial_specs = label.Label(
114 MEDIUM_FONT,
115 text="CircuitPython",
116 x=display.width // 2,
117 y=display.height // 2,
118 color=0xFFFFFF,
119)
120main_group.append(text_initial_specs)
121display.root_group = main_group
122time.sleep(TIME_PAUSE)
123
124text_initial_specs.background_color = 0x990099
125display.root_group = main_group
126time.sleep(TIME_PAUSE)
127main_group.pop()
128
129# Testing Background Color
130text_area.text = "Testing Background Tight"
131text_initial_specs = label.Label(
132 BIG_FONT,
133 text="aaaaq~",
134 x=0,
135 y=display.height // 2,
136 color=0xFFFFFF,
137 background_color=0x990099,
138 background_tight=True,
139)
140main_group.append(text_initial_specs)
141text_initial_specs = label.Label(
142 BIG_FONT,
143 text="aaaaq~",
144 x=90,
145 y=display.height // 2,
146 color=0xFFFFFF,
147 background_color=0x990099,
148 background_tight=False,
149)
150main_group.append(text_initial_specs)
151display.root_group = main_group
152time.sleep(TIME_PAUSE)
153main_group.pop()
154main_group.pop()
155
156# Testing Padding
157text_area.text = "Testing Padding"
158text_initial_specs = label.Label(
159 BIG_FONT,
160 text="CircuitPython",
161 x=display.width // 4,
162 y=display.height // 2,
163 color=0xFFFFFF,
164 background_color=0x990099,
165 padding_right=10,
166 padding_top=10,
167 padding_bottom=10,
168 padding_left=10,
169)
170main_group.append(text_initial_specs)
171display.root_group = main_group
172time.sleep(TIME_PAUSE)
173main_group.pop()
174
175# Testing Anchor Point/ Anchored Position
176text_area.text = "Testing Anchor Point/Anchored Position"
177text_initial_specs = label.Label(
178 MEDIUM_FONT,
179 text="CircuitPython",
180 x=display.width // 2,
181 y=display.height // 2,
182 color=0xFFFFFF,
183 background_color=0x990099,
184 padding_right=10,
185 padding_top=10,
186 padding_bottom=10,
187 padding_left=10,
188)
189main_group.append(text_initial_specs)
190display.root_group = main_group
191time.sleep(TIME_PAUSE)
192
193try:
194 text_initial_specs.anchored_position = (100, 100)
195 text_initial_specs.anchor_point = (0.5, 0.5)
196
197except TypeError:
198 print("Test is failing here")
199 main_group.pop()
200 warning_text = label.Label(
201 BIG_FONT,
202 text="Test Fail",
203 x=display.width // 2,
204 y=display.height // 4,
205 background_color=0x004499,
206 )
207 main_group.append(warning_text)
208 time.sleep(TIME_PAUSE)
209 display.root_group = main_group
210
211main_group.pop()
212
213# Testing Scale
214text_area.text = "Testing Scale"
215text_initial_specs = label.Label(
216 MEDIUM_FONT,
217 text="CircuitPython",
218 x=display.width // 2,
219 y=display.height // 2,
220 color=0xFFFFFF,
221 background_color=0x990099,
222 padding_right=10,
223 padding_top=10,
224 padding_bottom=10,
225 padding_left=10,
226 anchored_position=(display.width // 2, display.height // 2),
227 anchor_point=(0.5, 0.5),
228)
229main_group.append(text_initial_specs)
230display.root_group = main_group
231time.sleep(TIME_PAUSE)
232
233text_initial_specs.scale = 2
234display.root_group = main_group
235time.sleep(TIME_PAUSE)
236main_group.pop()
237
238# Testing Base Alignment
239text_area.text = "Testing Base Alignment"
240text_initial_specs = label.Label(
241 MEDIUM_FONT,
242 text="python",
243 x=display.width // 2,
244 y=display.height // 2,
245 color=0xFFFFFF,
246 background_color=0x990099,
247 base_alignment=True,
248)
249main_group.append(text_initial_specs)
250text_initial_specs = label.Label(
251 BIG_FONT,
252 text="circuit",
253 x=display.width // 2 - 100,
254 y=display.height // 2,
255 color=0xFFFFFF,
256 background_color=0x990099,
257 base_alignment=True,
258)
259main_group.append(text_initial_specs)
260display.root_group = main_group
261time.sleep(TIME_PAUSE)
262main_group.pop()
263main_group.pop()
264
265# Testing Direction
266text_area.text = "Testing Direction-UPR"
267text_initial_specs = label.Label(
268 MEDIUM_FONT,
269 text="CircuitPython",
270 x=display.width // 2,
271 y=display.height // 2,
272 color=0xFFFFFF,
273 background_color=0x990099,
274 padding_right=10,
275 padding_top=10,
276 padding_bottom=10,
277 padding_left=10,
278 anchored_position=(display.width // 2, display.height // 2),
279 anchor_point=(0.5, 0.5),
280 label_direction="UPR",
281)
282main_group.append(text_initial_specs)
283display.root_group = main_group
284time.sleep(TIME_PAUSE)
285main_group.pop()
286
287text_area.text = "Testing Direction-DWR"
288text_initial_specs = label.Label(
289 MEDIUM_FONT,
290 text="CircuitPython",
291 x=display.width // 2,
292 y=display.height // 2,
293 color=0xFFFFFF,
294 background_color=0x990099,
295 padding_right=10,
296 padding_top=10,
297 padding_bottom=10,
298 padding_left=10,
299 anchored_position=(display.width // 2, display.height // 2),
300 anchor_point=(0.5, 0.5),
301 label_direction="DWR",
302)
303main_group.append(text_initial_specs)
304display.root_group = main_group
305time.sleep(TIME_PAUSE)
306main_group.pop()
307
308text_area.text = "Testing Direction-TTB"
309text_initial_specs = label.Label(
310 MEDIUM_FONT,
311 text="CircuitPython",
312 x=display.width // 2,
313 y=display.height // 2,
314 color=0xFFFFFF,
315 background_color=0x990099,
316 padding_right=10,
317 padding_top=10,
318 padding_bottom=10,
319 padding_left=10,
320 anchored_position=(display.width // 2, display.height // 2),
321 anchor_point=(0.5, 0.5),
322 label_direction="TTB",
323)
324main_group.append(text_initial_specs)
325display.root_group = main_group
326time.sleep(TIME_PAUSE)
327main_group.pop()
328
329text_area.text = "Testing Direction-RTL"
330text_initial_specs = label.Label(
331 MEDIUM_FONT,
332 text="CircuitPython",
333 x=display.width // 2,
334 y=display.height // 2,
335 color=0xFFFFFF,
336 background_color=0x990099,
337 padding_right=10,
338 padding_top=10,
339 padding_bottom=10,
340 padding_left=10,
341 anchored_position=(display.width // 2, display.height // 2),
342 anchor_point=(0.5, 0.5),
343 label_direction="RTL",
344)
345main_group.append(text_initial_specs)
346display.root_group = main_group
347time.sleep(TIME_PAUSE)
348main_group.pop()
349
350main_group.pop()
351
352# Testing creating label with initial position
353display.root_group = main_group
354time.sleep(TIME_PAUSE)
355text_area = bitmap_label.Label(terminalio.FONT, text="Circuit Python")
356main_group.append(text_area)
357display.root_group = main_group
358time.sleep(TIME_PAUSE)
359# Testing position setter
360text_area.x = 10
361text_area.y = 10
362display.root_group = main_group
363time.sleep(TIME_PAUSE)
364text_area.text = "Testing initiating without text"
365try:
366 text_middle = label.Label(terminalio.FONT)
367except TypeError:
368 print("Fail setting-up label without text")
369 warning_text = label.Label(
370 BIG_FONT,
371 text="Test Fail",
372 x=display.width // 2,
373 y=display.height // 4,
374 background_color=0x004499,
375 )
376 main_group.append(warning_text)
377
378# Testing creating label with initial position
379text_area.text = "Testing Position"
380text_middle = bitmap_label.Label(
381 terminalio.FONT, text="Circuit", x=display.width // 2, y=display.height // 2
382)
383main_group.append(text_middle)
384display.root_group = main_group
385time.sleep(TIME_PAUSE)
386
387# Testing Text Setter
388text_area.text = "Testing Changing Text"
389text_middle.text = "Python"
390display.root_group = main_group
391time.sleep(TIME_PAUSE)
392
393# Testing a and y getter and setter
394text_area.text = "Testing Changing Position"
395text_middle.x = text_middle.x - 50
396text_middle.y = text_middle.y - 50
397display.root_group = main_group
398time.sleep(TIME_PAUSE)
399
400# Testing font Getter and setter
401text_area.text = "Testing Changing FONT"
402if isinstance(text_middle.font, fontio.BuiltinFont):
403 print("Font was BuiltinFont")
404 text_middle.font = MEDIUM_FONT
405display.root_group = main_group
406time.sleep(TIME_PAUSE)
407
408# Once this working we create another label with all the initial specs
409main_group.pop()
410
411# Testing Color
412text_area.text = "Testing Color"
413text_initial_specs = bitmap_label.Label(
414 MEDIUM_FONT,
415 text="Circuit Python",
416 x=display.width // 2,
417 y=display.height // 2,
418)
419main_group.append(text_initial_specs)
420display.root_group = main_group
421time.sleep(TIME_PAUSE)
422
423text_initial_specs.color = 0x004400
424display.root_group = main_group
425time.sleep(TIME_PAUSE)
426main_group.pop()
427
428# Testing Background Color
429text_area.text = "Testing Background Color"
430text_initial_specs = bitmap_label.Label(
431 MEDIUM_FONT,
432 text="CircuitPython",
433 x=display.width // 2,
434 y=display.height // 2,
435 color=0xFFFFFF,
436)
437main_group.append(text_initial_specs)
438display.root_group = main_group
439time.sleep(TIME_PAUSE)
440
441text_initial_specs.background_color = 0x990099
442display.root_group = main_group
443time.sleep(TIME_PAUSE)
444main_group.pop()
445
446# Testing Background Color
447text_area.text = "Testing Background Tight"
448text_initial_specs = bitmap_label.Label(
449 BIG_FONT,
450 text="aaaaq~",
451 x=0,
452 y=display.height // 2,
453 color=0xFFFFFF,
454 background_color=0x990099,
455 background_tight=True,
456)
457main_group.append(text_initial_specs)
458text_initial_specs = bitmap_label.Label(
459 BIG_FONT,
460 text="aaaaq~",
461 x=90,
462 y=display.height // 2,
463 color=0xFFFFFF,
464 background_color=0x990099,
465 background_tight=False,
466)
467main_group.append(text_initial_specs)
468display.root_group = main_group
469time.sleep(TIME_PAUSE)
470main_group.pop()
471main_group.pop()
472
473# Testing Padding
474text_area.text = "Testing Padding"
475text_initial_specs = bitmap_label.Label(
476 BIG_FONT,
477 text="CircuitPython",
478 x=display.width // 4,
479 y=display.height // 2,
480 color=0xFFFFFF,
481 background_color=0x990099,
482 padding_right=10,
483 padding_top=10,
484 padding_bottom=10,
485 padding_left=10,
486)
487main_group.append(text_initial_specs)
488display.root_group = main_group
489time.sleep(TIME_PAUSE)
490main_group.pop()
491
492# Testing Anchor Point/ Anchored Position
493text_area.text = "Testing Anchor Point/Anchored Position"
494text_initial_specs = bitmap_label.Label(
495 MEDIUM_FONT,
496 text="CircuitPython",
497 x=display.width // 2,
498 y=display.height // 2,
499 color=0xFFFFFF,
500 background_color=0x990099,
501 padding_right=10,
502 padding_top=10,
503 padding_bottom=10,
504 padding_left=10,
505)
506main_group.append(text_initial_specs)
507display.root_group = main_group
508time.sleep(TIME_PAUSE)
509
510try:
511 text_initial_specs.anchored_position = (100, 100)
512 text_initial_specs.anchor_point = (0.5, 0.5)
513
514except TypeError:
515 print("Test is failing here")
516 main_group.pop()
517 warning_text = bitmap_label.Label(
518 BIG_FONT,
519 text="Test Fail",
520 x=display.width // 2,
521 y=display.height // 4,
522 background_color=0x004499,
523 )
524 main_group.append(warning_text)
525 time.sleep(TIME_PAUSE)
526 display.root_group = main_group
527
528main_group.pop()
529
530# Testing Scale
531text_area.text = "Testing Scale"
532text_initial_specs = bitmap_label.Label(
533 MEDIUM_FONT,
534 text="CircuitPython",
535 x=display.width // 2,
536 y=display.height // 2,
537 color=0xFFFFFF,
538 background_color=0x990099,
539 padding_right=10,
540 padding_top=10,
541 padding_bottom=10,
542 padding_left=10,
543 anchored_position=(display.width // 2, display.height // 2),
544 anchor_point=(0.5, 0.5),
545)
546main_group.append(text_initial_specs)
547display.root_group = main_group
548time.sleep(TIME_PAUSE)
549
550text_initial_specs.scale = 2
551display.root_group = main_group
552time.sleep(TIME_PAUSE)
553main_group.pop()
554
555# Testing Base Alignment
556text_area.text = "Testing Base Alignment"
557text_initial_specs = bitmap_label.Label(
558 MEDIUM_FONT,
559 text="python",
560 x=display.width // 2,
561 y=display.height // 2,
562 color=0xFFFFFF,
563 background_color=0x990099,
564 base_alignment=True,
565)
566main_group.append(text_initial_specs)
567text_initial_specs = bitmap_label.Label(
568 BIG_FONT,
569 text="circuit",
570 x=display.width // 2 - 100,
571 y=display.height // 2,
572 color=0xFFFFFF,
573 background_color=0x990099,
574 base_alignment=True,
575)
576main_group.append(text_initial_specs)
577display.root_group = main_group
578time.sleep(TIME_PAUSE)
579main_group.pop()
580main_group.pop()
581
582# Testing Direction
583text_area.text = "Testing Direction-UPR"
584text_initial_specs = bitmap_label.Label(
585 MEDIUM_FONT,
586 text="CircuitPython",
587 x=display.width // 2,
588 y=display.height // 2,
589 color=0xFFFFFF,
590 background_color=0x990099,
591 padding_right=10,
592 padding_top=10,
593 padding_bottom=10,
594 padding_left=10,
595 anchored_position=(display.width // 2, display.height // 2),
596 anchor_point=(0.5, 0.5),
597 label_direction="UPR",
598)
599main_group.append(text_initial_specs)
600display.root_group = main_group
601time.sleep(TIME_PAUSE)
602main_group.pop()
603
604text_area.text = "Testing Direction-DWR"
605text_initial_specs = bitmap_label.Label(
606 MEDIUM_FONT,
607 text="CircuitPython",
608 x=display.width // 2,
609 y=display.height // 2,
610 color=0xFFFFFF,
611 background_color=0x990099,
612 padding_right=10,
613 padding_top=10,
614 padding_bottom=10,
615 padding_left=10,
616 anchored_position=(display.width // 2, display.height // 2),
617 anchor_point=(0.5, 0.5),
618 label_direction="DWR",
619)
620main_group.append(text_initial_specs)
621display.root_group = main_group
622time.sleep(TIME_PAUSE)
623main_group.pop()
624
625text_area.text = "Testing Direction-UPD"
626text_initial_specs = bitmap_label.Label(
627 MEDIUM_FONT,
628 text="CircuitPython",
629 x=display.width // 2,
630 y=display.height // 2,
631 color=0xFFFFFF,
632 background_color=0x990099,
633 padding_right=10,
634 padding_top=10,
635 padding_bottom=10,
636 padding_left=10,
637 anchored_position=(display.width // 2, display.height // 2),
638 anchor_point=(0.5, 0.5),
639 label_direction="UPD",
640)
641main_group.append(text_initial_specs)
642display.root_group = main_group
643time.sleep(TIME_PAUSE)
644main_group.pop()
645
646text_area.text = "Testing Direction-RTL"
647text_initial_specs = bitmap_label.Label(
648 MEDIUM_FONT,
649 text="CircuitPython",
650 x=display.width // 2,
651 y=display.height // 2,
652 color=0xFFFFFF,
653 background_color=0x990099,
654 padding_right=10,
655 padding_top=10,
656 padding_bottom=10,
657 padding_left=10,
658 anchored_position=(display.width // 2, display.height // 2),
659 anchor_point=(0.5, 0.5),
660 label_direction="RTL",
661)
662main_group.append(text_initial_specs)
663display.root_group = main_group
664time.sleep(TIME_PAUSE)
665main_group.pop()
666
667text_area.text = "Finished"
668print("Tests finished")