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 board
 8import displayio
 9import terminalio
10import adafruit_touchscreen
11from adafruit_button import Button
12
13# use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.)
14# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
15# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
16display = board.DISPLAY
17
18# --| Button Config |-------------------------------------------------
19BUTTON_X = 110
20BUTTON_Y = 95
21BUTTON_WIDTH = 100
22BUTTON_HEIGHT = 50
23BUTTON_STYLE = Button.ROUNDRECT
24BUTTON_FILL_COLOR = 0x00FFFF
25BUTTON_OUTLINE_COLOR = 0xFF00FF
26BUTTON_LABEL = "HELLO WORLD"
27BUTTON_LABEL_COLOR = 0x000000
28# --| Button Config |-------------------------------------------------
29
30# Setup touchscreen (PyPortal)
31ts = adafruit_touchscreen.Touchscreen(
32    board.TOUCH_XL,
33    board.TOUCH_XR,
34    board.TOUCH_YD,
35    board.TOUCH_YU,
36    calibration=((5200, 59000), (5800, 57000)),
37    size=(display.width, display.height),
38)
39
40# Make the display context
41splash = displayio.Group()
42display.root_group = splash
43
44# Make the button
45button = Button(
46    x=BUTTON_X,
47    y=BUTTON_Y,
48    width=BUTTON_WIDTH,
49    height=BUTTON_HEIGHT,
50    style=BUTTON_STYLE,
51    fill_color=BUTTON_FILL_COLOR,
52    outline_color=BUTTON_OUTLINE_COLOR,
53    label=BUTTON_LABEL,
54    label_font=terminalio.FONT,
55    label_color=BUTTON_LABEL_COLOR,
56)
57
58# Add button to the display context
59splash.append(button)
60
61# Loop and look for touches
62while True:
63    p = ts.touch_point
64    if p:
65        if button.contains(p):
66            button.selected = True
67        else:
68            button.selected = False  # if touch is dragged outside of button
69    else:
70        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 board
10import displayio
11import terminalio
12import adafruit_touchscreen
13from adafruit_button import Button
14
15# use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.)
16# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
17# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
18display = board.DISPLAY
19
20# --| Button Config |-------------------------------------------------
21BUTTON_X = 110
22BUTTON_Y = 95
23BUTTON_WIDTH = 100
24BUTTON_HEIGHT = 50
25BUTTON_STYLE = Button.ROUNDRECT
26BUTTON_FILL_COLOR = 0xAA0000
27BUTTON_OUTLINE_COLOR = 0x0000FF
28BUTTON_LABEL = "HELLO WORLD"
29BUTTON_LABEL_COLOR = 0x000000
30# --| Button Config |-------------------------------------------------
31
32# Setup touchscreen (PyPortal)
33ts = adafruit_touchscreen.Touchscreen(
34    board.TOUCH_XL,
35    board.TOUCH_XR,
36    board.TOUCH_YD,
37    board.TOUCH_YU,
38    calibration=((5200, 59000), (5800, 57000)),
39    size=(display.width, display.height),
40)
41
42# Make the display context
43splash = displayio.Group()
44display.root_group = splash
45
46# Make the button
47button = Button(
48    x=BUTTON_X,
49    y=BUTTON_Y,
50    width=BUTTON_WIDTH,
51    height=BUTTON_HEIGHT,
52    style=BUTTON_STYLE,
53    fill_color=BUTTON_FILL_COLOR,
54    outline_color=BUTTON_OUTLINE_COLOR,
55    label="HELLO WORLD",
56    label_font=terminalio.FONT,
57    label_color=BUTTON_LABEL_COLOR,
58)
59
60button.fill_color = 0x00FF00
61button.outline_color = 0xFF0000
62
63button.selected_fill = (0, 0, 255)
64button.selected_outline = (255, 0, 0)
65
66button.label_color = 0xFF0000
67button.selected_label = 0x00FF00
68
69# Add button to the display context
70splash.append(button)
71
72# Loop and look for touches
73while True:
74    p = ts.touch_point
75    if p:
76        if button.contains(p):
77            print(p)
78            button.selected = True
79    else:
80        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
  8import board
  9import displayio
 10from adafruit_bitmap_font import bitmap_font
 11import adafruit_touchscreen
 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# These pins are used as both analog and digital! XL, XR and YU must be analog
 20# and digital capable. YD just need to be digital
 21ts = adafruit_touchscreen.Touchscreen(
 22    board.TOUCH_XL,
 23    board.TOUCH_XR,
 24    board.TOUCH_YD,
 25    board.TOUCH_YU,
 26    calibration=((5200, 59000), (5800, 57000)),
 27    size=(display.width, display.height),
 28)
 29
 30# the current working directory (where this file is)
 31cwd = ("/" + __file__).rsplit("/", 1)[0]
 32fonts = [
 33    file
 34    for file in os.listdir(cwd + "/fonts/")
 35    if (file.endswith(".bdf") and not file.startswith("._"))
 36]
 37for i, filename in enumerate(fonts):
 38    fonts[i] = cwd + "/fonts/" + filename
 39print(fonts)
 40THE_FONT = "/fonts/Arial-12.bdf"
 41DISPLAY_STRING = "Button Text"
 42
 43# Make the display context
 44splash = displayio.Group()
 45display.root_group = splash
 46BUTTON_WIDTH = 80
 47BUTTON_HEIGHT = 40
 48BUTTON_MARGIN = 20
 49
 50##########################################################################
 51# Make a background color fill
 52
 53color_bitmap = displayio.Bitmap(display.width, display.height, 1)
 54color_palette = displayio.Palette(1)
 55color_palette[0] = 0x404040
 56bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
 57print(bg_sprite.x, bg_sprite.y)
 58splash.append(bg_sprite)
 59
 60##########################################################################
 61
 62# Load the font
 63font = bitmap_font.load_font(THE_FONT)
 64
 65buttons = []
 66# Default button styling:
 67button_0 = Button(
 68    x=BUTTON_MARGIN,
 69    y=BUTTON_MARGIN,
 70    width=BUTTON_WIDTH,
 71    height=BUTTON_HEIGHT,
 72    label="button0",
 73    label_font=font,
 74)
 75buttons.append(button_0)
 76
 77# a button with no indicators at all
 78button_1 = Button(
 79    x=BUTTON_MARGIN * 2 + BUTTON_WIDTH,
 80    y=BUTTON_MARGIN,
 81    width=BUTTON_WIDTH,
 82    height=BUTTON_HEIGHT,
 83    fill_color=None,
 84    outline_color=None,
 85)
 86buttons.append(button_1)
 87
 88# various colorings
 89button_2 = Button(
 90    x=BUTTON_MARGIN * 3 + 2 * BUTTON_WIDTH,
 91    y=BUTTON_MARGIN,
 92    width=BUTTON_WIDTH,
 93    height=BUTTON_HEIGHT,
 94    label="button2",
 95    label_font=font,
 96    label_color=0x0000FF,
 97    fill_color=0x00FF00,
 98    outline_color=0xFF0000,
 99)
100buttons.append(button_2)
101
102# Transparent button with text
103button_3 = Button(
104    x=BUTTON_MARGIN,
105    y=BUTTON_MARGIN * 2 + BUTTON_HEIGHT,
106    width=BUTTON_WIDTH,
107    height=BUTTON_HEIGHT,
108    label="button3",
109    label_font=font,
110    label_color=0x0,
111    fill_color=None,
112    outline_color=None,
113)
114buttons.append(button_3)
115
116# a roundrect
117button_4 = Button(
118    x=BUTTON_MARGIN * 2 + BUTTON_WIDTH,
119    y=BUTTON_MARGIN * 2 + BUTTON_HEIGHT,
120    width=BUTTON_WIDTH,
121    height=BUTTON_HEIGHT,
122    label="button4",
123    label_font=font,
124    style=Button.ROUNDRECT,
125)
126buttons.append(button_4)
127
128# a shadowrect
129button_5 = Button(
130    x=BUTTON_MARGIN * 3 + BUTTON_WIDTH * 2,
131    y=BUTTON_MARGIN * 2 + BUTTON_HEIGHT,
132    width=BUTTON_WIDTH,
133    height=BUTTON_HEIGHT,
134    label="button5",
135    label_font=font,
136    style=Button.SHADOWRECT,
137)
138buttons.append(button_5)
139
140# a shadowroundrect
141button_6 = Button(
142    x=BUTTON_MARGIN,
143    y=BUTTON_MARGIN * 3 + BUTTON_HEIGHT * 2,
144    width=BUTTON_WIDTH,
145    height=BUTTON_HEIGHT,
146    label="button6",
147    label_font=font,
148    style=Button.SHADOWROUNDRECT,
149)
150buttons.append(button_6)
151
152for b in buttons:
153    splash.append(b)
154
155while True:
156    p = ts.touch_point
157    if p:
158        print(p)
159        for i, b in enumerate(buttons):
160            if b.contains(p):
161                print("Button %d pressed" % i)
162                b.selected = True
163            else:
164                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
 8from adafruit_pyportal import PyPortal
 9from adafruit_button import Button
10
11SHOW_BUTTONS = False
12
13# the current working directory (where this file is)
14cwd = ("/" + __file__).rsplit("/", 1)[0]
15# No internet use version of pyportal
16pyportal = PyPortal(default_bg=cwd + "/button_background.bmp")
17
18spots = []
19spots.append({"label": "1", "pos": (10, 10), "size": (60, 60), "file": "01.wav"})
20spots.append({"label": "2", "pos": (90, 10), "size": (60, 60), "file": "02.wav"})
21spots.append({"label": "3", "pos": (170, 10), "size": (60, 60), "file": "03.wav"})
22spots.append({"label": "4", "pos": (250, 10), "size": (60, 60), "file": "04.wav"})
23spots.append({"label": "5", "pos": (10, 90), "size": (60, 60), "file": "05.wav"})
24spots.append({"label": "6", "pos": (90, 90), "size": (60, 60), "file": "06.wav"})
25spots.append({"label": "7", "pos": (170, 90), "size": (60, 60), "file": "07.wav"})
26spots.append({"label": "8", "pos": (250, 90), "size": (60, 60), "file": "08.wav"})
27spots.append({"label": "9", "pos": (10, 170), "size": (60, 60), "file": "09.wav"})
28spots.append({"label": "10", "pos": (90, 170), "size": (60, 60), "file": "10.wav"})
29spots.append({"label": "11", "pos": (170, 170), "size": (60, 60), "file": "11.wav"})
30spots.append({"label": "12", "pos": (250, 170), "size": (60, 60), "file": "12.wav"})
31
32buttons = []
33for spot in spots:
34    fill = outline = None
35    if SHOW_BUTTONS:
36        fill = None
37        outline = 0x00FF00
38    button = Button(
39        x=spot["pos"][0],
40        y=spot["pos"][1],
41        width=spot["size"][0],
42        height=spot["size"][1],
43        fill_color=fill,
44        outline_color=outline,
45        label=spot["label"],
46        label_color=None,
47        name=spot["file"],
48    )
49    pyportal.splash.append(button)
50    buttons.append(button)
51
52last_pressed = None
53currently_pressed = None
54while True:
55    p = pyportal.touchscreen.touch_point
56    if p:
57        print(p)
58        for b in buttons:
59            if b.contains(p):
60                print("Touched", b.name)
61                if currently_pressed != b:  # don't restart if playing
62                    pyportal.play_file(cwd + "/" + b.name, wait_to_finish=False)
63                currently_pressed = b
64                break
65        else:
66            currently_pressed = None
67    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
 4import board
 5import displayio
 6import adafruit_touchscreen
 7import terminalio
 8from adafruit_button.sprite_button import SpriteButton
 9
10# These pins are used as both analog and digital! XL, XR and YU must be analog
11# and digital capable. YD just need to be digital
12ts = adafruit_touchscreen.Touchscreen(
13    board.TOUCH_XL,
14    board.TOUCH_XR,
15    board.TOUCH_YD,
16    board.TOUCH_YU,
17    calibration=((5200, 59000), (5800, 57000)),
18    size=(board.DISPLAY.width, board.DISPLAY.height),
19)
20
21# Make the display context
22main_group = displayio.Group()
23board.DISPLAY.root_group = main_group
24
25BUTTON_WIDTH = 10 * 16
26BUTTON_HEIGHT = 3 * 16
27BUTTON_MARGIN = 20
28
29font = terminalio.FONT
30
31buttons = []
32
33
34button_0 = SpriteButton(
35    x=BUTTON_MARGIN,
36    y=BUTTON_MARGIN,
37    width=BUTTON_WIDTH,
38    height=BUTTON_HEIGHT,
39    label="button0",
40    label_font=font,
41    bmp_path="bmps/gradient_button_0.bmp",
42    selected_bmp_path="bmps/gradient_button_1.bmp",
43    transparent_index=0,
44)
45
46buttons.append(button_0)
47
48for b in buttons:
49    main_group.append(b)
50while True:
51    p = ts.touch_point
52    if p:
53        print(p)
54        for i, b in enumerate(buttons):
55            if b.contains(p):
56                print("Button %d pressed" % i)
57                b.selected = True
58                b.label = "pressed"
59            else:
60                b.selected = False
61                b.label = "button0"
62
63    else:
64        for i, b in enumerate(buttons):
65            if b.selected:
66                b.selected = False
67                b.label = "button0"
68    time.sleep(0.01)