/ examples / display_text_background_color_padding.py
display_text_background_color_padding.py
  1  """
  2  This examples shows the use color and background_color
  3  """
  4  import time
  5  import board
  6  import displayio
  7  
  8  
  9  # from adafruit_st7789 import ST7789
 10  from adafruit_ili9341 import ILI9341
 11  from adafruit_bitmap_font import bitmap_font
 12  from adafruit_display_text import label
 13  
 14  #  Setup the SPI display
 15  
 16  print("Starting the display...")  # goes to serial only
 17  displayio.release_displays()
 18  
 19  
 20  spi = board.SPI()
 21  tft_cs = board.D9  # arbitrary, pin not used
 22  tft_dc = board.D10
 23  tft_backlight = board.D12
 24  tft_reset = board.D11
 25  
 26  while not spi.try_lock():
 27      spi.configure(baudrate=32000000)
 28  spi.unlock()
 29  
 30  display_bus = displayio.FourWire(
 31      spi,
 32      command=tft_dc,
 33      chip_select=tft_cs,
 34      reset=tft_reset,
 35      baudrate=32000000,
 36      polarity=1,
 37      phase=1,
 38  )
 39  
 40  print("spi.frequency: {}".format(spi.frequency))
 41  
 42  DISPLAY_WIDTH = 320
 43  DISPLAY_HEIGHT = 240
 44  
 45  # display = ST7789(display_bus, width=240, height=240, rotation=0, rowstart=80, colstart=0)
 46  display = ILI9341(
 47      display_bus,
 48      width=DISPLAY_WIDTH,
 49      height=DISPLAY_HEIGHT,
 50      rotation=180,
 51      auto_refresh=True,
 52  )
 53  
 54  display.show(None)
 55  
 56  # font=terminalio.FONT # this is the Builtin fixed dimension font
 57  
 58  font = bitmap_font.load_font("fonts/Helvetica-Bold-16.bdf")
 59  
 60  
 61  text = []
 62  text.append("none")  # no ascenders or descenders
 63  text.append("pop quops")  # only descenders
 64  text.append("MONSTERs are tall")  # only ascenders
 65  text.append("MONSTERs ate pop quops")  # both ascenders and descenders
 66  text.append("MONSTER quops\nnewline quops")  # with newline
 67  
 68  display.auto_refresh = True
 69  myGroup = displayio.Group(max_size=6)
 70  display.show(myGroup)
 71  
 72  text_area = []
 73  myPadding = 4
 74  
 75  for i, thisText in enumerate(text):
 76      text_area.append(
 77          label.Label(
 78              font,
 79              text=thisText,
 80              color=0xFFFFFF,
 81              background_color=None,
 82              background_tight=False,
 83              padding_top=myPadding,
 84              padding_bottom=myPadding,
 85              padding_left=myPadding,
 86              padding_right=myPadding,
 87          )
 88      )
 89  
 90      this_x = 10
 91      this_y = 10 + i * 40
 92      text_area[i].x = 10
 93      text_area[i].y = 3 + i * 50
 94      text_area[i].anchor_point = (0, 0)
 95      text_area[i].anchored_position = (this_x, this_y)
 96      myGroup.append(text_area[i])
 97  
 98  print("background color is {}".format(text_area[0].background_color))
 99  
100  
101  while True:
102      time.sleep(2)
103      text_area[0].text = "text"  # change some text in an existing text box
104      # Note: changed text must fit within existing number of characters
105      # when the Label was created
106  
107      for area in text_area:
108          area.background_color = 0xFF0000
109      print("background color is {:06x}".format(text_area[0].background_color))
110      time.sleep(2)
111      for area in text_area:
112          area.background_color = 0x000088
113      print("background color is {:06x}".format(text_area[0].background_color))
114      time.sleep(2)
115      for area in text_area:
116          area.background_color = 0x00FF00
117      print("background color is {:06x}".format(text_area[0].background_color))
118      time.sleep(2)
119      for area in text_area:
120          area.background_color = 0xFF0000
121      print("background color is {:06x}".format(text_area[0].background_color))
122      time.sleep(2)
123      for area in text_area:
124          area.background_color = None
125      print("background color is {}".format(text_area[0].background_color))