Simple test

Ensure your device works with this simple test.

examples/display_button_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3"""
 4Simple button example.
 5"""
 6
 7import adafruit_touchscreen
 8import board
 9import displayio
10import terminalio
11
12from adafruit_button import Button
13
14# use built in display (MagTag, 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# --| Button Config |-------------------------------------------------
20BUTTON_X = 110
21BUTTON_Y = 95
22BUTTON_WIDTH = 100
23BUTTON_HEIGHT = 50
24BUTTON_STYLE = Button.ROUNDRECT
25BUTTON_FILL_COLOR = 0x00FFFF
26BUTTON_OUTLINE_COLOR = 0xFF00FF
27BUTTON_LABEL = "HELLO WORLD"
28BUTTON_LABEL_COLOR = 0x000000
29# --| Button Config |-------------------------------------------------
30
31# Setup touchscreen (PyPortal)
32ts = adafruit_touchscreen.Touchscreen(
33    board.TOUCH_XL,
34    board.TOUCH_XR,
35    board.TOUCH_YD,
36    board.TOUCH_YU,
37    calibration=((5200, 59000), (5800, 57000)),
38    size=(display.width, display.height),
39)
40
41# Make the display context
42splash = displayio.Group()
43display.root_group = splash
44
45# Make the button
46button = Button(
47    x=BUTTON_X,
48    y=BUTTON_Y,
49    width=BUTTON_WIDTH,
50    height=BUTTON_HEIGHT,
51    style=BUTTON_STYLE,
52    fill_color=BUTTON_FILL_COLOR,
53    outline_color=BUTTON_OUTLINE_COLOR,
54    label=BUTTON_LABEL,
55    label_font=terminalio.FONT,
56    label_color=BUTTON_LABEL_COLOR,
57)
58
59# Add button to the display context
60splash.append(button)
61
62# Loop and look for touches
63while True:
64    p = ts.touch_point
65    if p:
66        if button.contains(p):
67            button.selected = True
68        else:
69            button.selected = False  # if touch is dragged outside of button
70    else:
71        button.selected = False  # if touch is released

Button Color Properties

Demonstrate the different color possibilities present in the library

examples/display_button_color_properties.py
 1# SPDX-FileCopyrightText: 2021 Tim Cocks for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5Basic example that illustrates how to set the various color options on the button using
 6properties after the button has been initialized.
 7"""
 8
 9import adafruit_touchscreen
10import board
11import displayio
12import terminalio
13
14from adafruit_button import Button
15
16# use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.)
17# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
18# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
19display = board.DISPLAY
20
21# --| Button Config |-------------------------------------------------
22BUTTON_X = 110
23BUTTON_Y = 95
24BUTTON_WIDTH = 100
25BUTTON_HEIGHT = 50
26BUTTON_STYLE = Button.ROUNDRECT
27BUTTON_FILL_COLOR = 0xAA0000
28BUTTON_OUTLINE_COLOR = 0x0000FF
29BUTTON_LABEL = "HELLO WORLD"
30BUTTON_LABEL_COLOR = 0x000000
31# --| Button Config |-------------------------------------------------
32
33# Setup touchscreen (PyPortal)
34ts = adafruit_touchscreen.Touchscreen(
35    board.TOUCH_XL,
36    board.TOUCH_XR,
37    board.TOUCH_YD,
38    board.TOUCH_YU,
39    calibration=((5200, 59000), (5800, 57000)),
40    size=(display.width, display.height),
41)
42
43# Make the display context
44splash = displayio.Group()
45display.root_group = splash
46
47# Make the button
48button = Button(
49    x=BUTTON_X,
50    y=BUTTON_Y,
51    width=BUTTON_WIDTH,
52    height=BUTTON_HEIGHT,
53    style=BUTTON_STYLE,
54    fill_color=BUTTON_FILL_COLOR,
55    outline_color=BUTTON_OUTLINE_COLOR,
56    label="HELLO WORLD",
57    label_font=terminalio.FONT,
58    label_color=BUTTON_LABEL_COLOR,
59)
60
61button.fill_color = 0x00FF00
62button.outline_color = 0xFF0000
63
64button.selected_fill = (0, 0, 255)
65button.selected_outline = (255, 0, 0)
66
67button.label_color = 0xFF0000
68button.selected_label = 0x00FF00
69
70# Add button to the display context
71splash.append(button)
72
73# Loop and look for touches
74while True:
75    p = ts.touch_point
76    if p:
77        if button.contains(p):
78            print(p)
79            button.selected = True
80    else:
81        button.selected = False

Button Custom Font

Shows how to use different fonts with your button

examples/display_button_customfont.py
  1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
  2# SPDX-License-Identifier: MIT
  3"""
  4Button example with a custom font.
  5"""
  6
  7import os
  8
  9import adafruit_touchscreen
 10import board
 11import displayio
 12from adafruit_bitmap_font import bitmap_font
 13
 14from adafruit_button import Button
 15
 16# use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.)
 17# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
 18# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
 19display = board.DISPLAY
 20
 21# These pins are used as both analog and digital! XL, XR and YU must be analog
 22# and digital capable. YD just need to be digital
 23ts = adafruit_touchscreen.Touchscreen(
 24    board.TOUCH_XL,
 25    board.TOUCH_XR,
 26    board.TOUCH_YD,
 27    board.TOUCH_YU,
 28    calibration=((5200, 59000), (5800, 57000)),
 29    size=(display.width, display.height),
 30)
 31
 32# the current working directory (where this file is)
 33cwd = ("/" + __file__).rsplit("/", 1)[0]
 34fonts = [
 35    file
 36    for file in os.listdir(cwd + "/fonts/")
 37    if (file.endswith(".bdf") and not file.startswith("._"))
 38]
 39for i, filename in enumerate(fonts):
 40    fonts[i] = cwd + "/fonts/" + filename
 41print(fonts)
 42THE_FONT = "/fonts/Arial-12.bdf"
 43DISPLAY_STRING = "Button Text"
 44
 45# Make the display context
 46splash = displayio.Group()
 47display.root_group = splash
 48BUTTON_WIDTH = 80
 49BUTTON_HEIGHT = 40
 50BUTTON_MARGIN = 20
 51
 52##########################################################################
 53# Make a background color fill
 54
 55color_bitmap = displayio.Bitmap(display.width, display.height, 1)
 56color_palette = displayio.Palette(1)
 57color_palette[0] = 0x404040
 58bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
 59print(bg_sprite.x, bg_sprite.y)
 60splash.append(bg_sprite)
 61
 62##########################################################################
 63
 64# Load the font
 65font = bitmap_font.load_font(THE_FONT)
 66
 67buttons = []
 68# Default button styling:
 69button_0 = Button(
 70    x=BUTTON_MARGIN,
 71    y=BUTTON_MARGIN,
 72    width=BUTTON_WIDTH,
 73    height=BUTTON_HEIGHT,
 74    label="button0",
 75    label_font=font,
 76)
 77buttons.append(button_0)
 78
 79# a button with no indicators at all
 80button_1 = Button(
 81    x=BUTTON_MARGIN * 2 + BUTTON_WIDTH,
 82    y=BUTTON_MARGIN,
 83    width=BUTTON_WIDTH,
 84    height=BUTTON_HEIGHT,
 85    fill_color=None,
 86    outline_color=None,
 87)
 88buttons.append(button_1)
 89
 90# various colorings
 91button_2 = Button(
 92    x=BUTTON_MARGIN * 3 + 2 * BUTTON_WIDTH,
 93    y=BUTTON_MARGIN,
 94    width=BUTTON_WIDTH,
 95    height=BUTTON_HEIGHT,
 96    label="button2",
 97    label_font=font,
 98    label_color=0x0000FF,
 99    fill_color=0x00FF00,
100    outline_color=0xFF0000,
101)
102buttons.append(button_2)
103
104# Transparent button with text
105button_3 = Button(
106    x=BUTTON_MARGIN,
107    y=BUTTON_MARGIN * 2 + BUTTON_HEIGHT,
108    width=BUTTON_WIDTH,
109    height=BUTTON_HEIGHT,
110    label="button3",
111    label_font=font,
112    label_color=0x0,
113    fill_color=None,
114    outline_color=None,
115)
116buttons.append(button_3)
117
118# a roundrect
119button_4 = Button(
120    x=BUTTON_MARGIN * 2 + BUTTON_WIDTH,
121    y=BUTTON_MARGIN * 2 + BUTTON_HEIGHT,
122    width=BUTTON_WIDTH,
123    height=BUTTON_HEIGHT,
124    label="button4",
125    label_font=font,
126    style=Button.ROUNDRECT,
127)
128buttons.append(button_4)
129
130# a shadowrect
131button_5 = Button(
132    x=BUTTON_MARGIN * 3 + BUTTON_WIDTH * 2,
133    y=BUTTON_MARGIN * 2 + BUTTON_HEIGHT,
134    width=BUTTON_WIDTH,
135    height=BUTTON_HEIGHT,
136    label="button5",
137    label_font=font,
138    style=Button.SHADOWRECT,
139)
140buttons.append(button_5)
141
142# a shadowroundrect
143button_6 = Button(
144    x=BUTTON_MARGIN,
145    y=BUTTON_MARGIN * 3 + BUTTON_HEIGHT * 2,
146    width=BUTTON_WIDTH,
147    height=BUTTON_HEIGHT,
148    label="button6",
149    label_font=font,
150    style=Button.SHADOWROUNDRECT,
151)
152buttons.append(button_6)
153
154for b in buttons:
155    splash.append(b)
156
157while True:
158    p = ts.touch_point
159    if p:
160        print(p)
161        for i, b in enumerate(buttons):
162            if b.contains(p):
163                print("Button %d pressed" % i)
164                b.selected = True
165            else:
166                b.selected = False

Soundboard

A soundboard made with buttons

examples/display_button_soundboard.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3"""
 4Soundboard example with buttons.
 5"""
 6
 7import time
 8
 9from adafruit_pyportal import PyPortal
10
11from adafruit_button import Button
12
13SHOW_BUTTONS = False
14
15# the current working directory (where this file is)
16cwd = ("/" + __file__).rsplit("/", 1)[0]
17# No internet use version of pyportal
18pyportal = PyPortal(default_bg=cwd + "/button_background.bmp")
19
20spots = []
21spots.append({"label": "1", "pos": (10, 10), "size": (60, 60), "file": "01.wav"})
22spots.append({"label": "2", "pos": (90, 10), "size": (60, 60), "file": "02.wav"})
23spots.append({"label": "3", "pos": (170, 10), "size": (60, 60), "file": "03.wav"})
24spots.append({"label": "4", "pos": (250, 10), "size": (60, 60), "file": "04.wav"})
25spots.append({"label": "5", "pos": (10, 90), "size": (60, 60), "file": "05.wav"})
26spots.append({"label": "6", "pos": (90, 90), "size": (60, 60), "file": "06.wav"})
27spots.append({"label": "7", "pos": (170, 90), "size": (60, 60), "file": "07.wav"})
28spots.append({"label": "8", "pos": (250, 90), "size": (60, 60), "file": "08.wav"})
29spots.append({"label": "9", "pos": (10, 170), "size": (60, 60), "file": "09.wav"})
30spots.append({"label": "10", "pos": (90, 170), "size": (60, 60), "file": "10.wav"})
31spots.append({"label": "11", "pos": (170, 170), "size": (60, 60), "file": "11.wav"})
32spots.append({"label": "12", "pos": (250, 170), "size": (60, 60), "file": "12.wav"})
33
34buttons = []
35for spot in spots:
36    fill = outline = None
37    if SHOW_BUTTONS:
38        fill = None
39        outline = 0x00FF00
40    button = Button(
41        x=spot["pos"][0],
42        y=spot["pos"][1],
43        width=spot["size"][0],
44        height=spot["size"][1],
45        fill_color=fill,
46        outline_color=outline,
47        label=spot["label"],
48        label_color=None,
49        name=spot["file"],
50    )
51    pyportal.splash.append(button)
52    buttons.append(button)
53
54last_pressed = None
55currently_pressed = None
56while True:
57    p = pyportal.touchscreen.touch_point
58    if p:
59        print(p)
60        for b in buttons:
61            if b.contains(p):
62                print("Touched", b.name)
63                if currently_pressed != b:  # don't restart if playing
64                    pyportal.play_file(cwd + "/" + b.name, wait_to_finish=False)
65                currently_pressed = b
66                break
67        else:
68            currently_pressed = None
69    time.sleep(0.05)

Sprite Button

Custom sprite button

examples/display_button_spritebutton_simpletest.py
 1# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3import time
 4
 5import adafruit_touchscreen
 6import board
 7import displayio
 8import terminalio
 9
10from adafruit_button.sprite_button import SpriteButton
11
12# These pins are used as both analog and digital! XL, XR and YU must be analog
13# and digital capable. YD just need to be digital
14ts = adafruit_touchscreen.Touchscreen(
15    board.TOUCH_XL,
16    board.TOUCH_XR,
17    board.TOUCH_YD,
18    board.TOUCH_YU,
19    calibration=((5200, 59000), (5800, 57000)),
20    size=(board.DISPLAY.width, board.DISPLAY.height),
21)
22
23# Make the display context
24main_group = displayio.Group()
25board.DISPLAY.root_group = main_group
26
27BUTTON_WIDTH = 10 * 16
28BUTTON_HEIGHT = 3 * 16
29BUTTON_MARGIN = 20
30
31font = terminalio.FONT
32
33buttons = []
34
35
36button_0 = SpriteButton(
37    x=BUTTON_MARGIN,
38    y=BUTTON_MARGIN,
39    width=BUTTON_WIDTH,
40    height=BUTTON_HEIGHT,
41    label="button0",
42    label_font=font,
43    bmp_path="bmps/gradient_button_0.bmp",
44    selected_bmp_path="bmps/gradient_button_1.bmp",
45    transparent_index=0,
46)
47
48buttons.append(button_0)
49
50for b in buttons:
51    main_group.append(b)
52while True:
53    p = ts.touch_point
54    if p:
55        print(p)
56        for i, b in enumerate(buttons):
57            if b.contains(p):
58                print("Button %d pressed" % i)
59                b.selected = True
60                b.label = "pressed"
61            else:
62                b.selected = False
63                b.label = "button0"
64
65    else:
66        for i, b in enumerate(buttons):
67            if b.selected:
68                b.selected = False
69                b.label = "button0"
70    time.sleep(0.01)