|
| 1 | +""" |
| 2 | +Basic display_text.label example script |
| 3 | +adapted for use on MagTag. |
| 4 | +""" |
| 5 | +import time |
| 6 | +import board |
| 7 | +import displayio |
| 8 | +import terminalio |
| 9 | +from adafruit_display_text import label |
| 10 | + |
| 11 | +# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.) |
| 12 | +# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.) |
| 13 | +# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus |
| 14 | +display = board.DISPLAY |
| 15 | + |
| 16 | +# wait until we can draw |
| 17 | +time.sleep(display.time_to_refresh) |
| 18 | + |
| 19 | +# main group to hold everything |
| 20 | +main_group = displayio.Group() |
| 21 | + |
| 22 | +# white background. Scaled to save RAM |
| 23 | +bg_bitmap = displayio.Bitmap(display.width // 8, display.height // 8, 1) |
| 24 | +bg_palette = displayio.Palette(1) |
| 25 | +bg_palette[0] = 0xFFFFFF |
| 26 | +bg_sprite = displayio.TileGrid(bg_bitmap, x=0, y=0, pixel_shader=bg_palette) |
| 27 | +bg_group = displayio.Group(scale=8) |
| 28 | +bg_group.append(bg_sprite) |
| 29 | +main_group.append(bg_group) |
| 30 | + |
| 31 | +# first example label |
| 32 | +TEXT = "Hello world" |
| 33 | +text_area = label.Label( |
| 34 | + terminalio.FONT, |
| 35 | + text=TEXT, |
| 36 | + color=0xFFFFFF, |
| 37 | + background_color=0x666666, |
| 38 | + padding_top=1, |
| 39 | + padding_bottom=3, |
| 40 | + padding_right=4, |
| 41 | + padding_left=4, |
| 42 | +) |
| 43 | +text_area.x = 10 |
| 44 | +text_area.y = 14 |
| 45 | +main_group.append(text_area) |
| 46 | + |
| 47 | +# second example label |
| 48 | +another_text = label.Label( |
| 49 | + terminalio.FONT, |
| 50 | + scale=2, |
| 51 | + text="MagTag display_text\nexample", |
| 52 | + color=0x000000, |
| 53 | + background_color=0x999999, |
| 54 | + padding_top=1, |
| 55 | + padding_bottom=3, |
| 56 | + padding_right=4, |
| 57 | + padding_left=4, |
| 58 | +) |
| 59 | +# centered |
| 60 | +another_text.anchor_point = (0.5, 0.5) |
| 61 | +another_text.anchored_position = (display.width // 2, display.height // 2) |
| 62 | +main_group.append(another_text) |
| 63 | + |
| 64 | +# show the main group and refresh. |
| 65 | +display.show(main_group) |
| 66 | +display.refresh() |
| 67 | +while True: |
| 68 | + pass |
0 commit comments