Inky pHAT not updating when buttons are pressed

Hello, i’m working on a project where whenever a button on the button shim is pressed, the inky pHAT displays a certain output text. This is the code:

from inky import InkyPHAT
from PIL import Image, ImageFont, ImageDraw
from font_fredoka_one import FredokaOne
import buttonshim
import signal

@buttonshim.on_release(buttonshim.BUTTON_A)
    def button_a(button, pressed):
        messageA = "Button A was pressed"
        print(messageA)
        buttonshim.set_pixel(255, 255, 255)

        inky_display = InkyPHAT("red")
        inky_display.set_border(inky_display.WHITE)
        img = Image.new("P", (inky_display.WIDTH, inky_display.HEIGHT))
        draw = ImageDraw.Draw(img)
        font = ImageFont.truetype(FredokaOne, 15)
        w, h = font.getsize(messageA)
        x = (inky_display.WIDTH / 2) - (w / 2)
        y = (inky_display.HEIGHT / 2) - (h / 2)
        draw.text((x, y), messageA, inky_display.BLACK, font)
        inky_display.set_image(img)
        inky_display.show()


@buttonshim.on_release(buttonshim.BUTTON_B)
def button_b(button, pressed):
    messageB = "Button B was pressed"
    
    
    inky_display = InkyPHAT("red")
    inky_display.set_border(inky_display.WHITE)
    
    img = Image.new("P", (inky_display.WIDTH, inky_display.HEIGHT))
    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype(FredokaOne, 15)
    w, h = font.getsize(messageB)
    x = (inky_display.WIDTH / 2) - (w / 2)
    y = (inky_display.HEIGHT / 2) - (h / 2)
    draw.text((x, y), messageB, inky_display.BLACK, font)
    inky_display.set_image(img)
    inky_display.show()
    print(messageB)
    buttonshim.set_pixel(255, 0, 0)
        
@buttonshim.on_release(buttonshim.BUTTON_C)
def button_c(button, pressed):
    messageC = "Button C was pressed"
    print(messageC)
    buttonshim.set_pixel(0, 0, 255)
    
    inky_display = InkyPHAT("red")
    inky_display.set_border(inky_display.WHITE)
    
    img = Image.new("P", (inky_display.WIDTH, inky_display.HEIGHT))
    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype(FredokaOne, 15)
    w, h = font.getsize(messageC)
    x = (inky_display.WIDTH / 2) - (w / 2)
    y = (inky_display.HEIGHT / 2) - (h / 2)
    draw.text((x, y), messageC, inky_display.BLACK, font)
    inky_display.set_image(img)
    inky_display.show()

@buttonshim.on_release(buttonshim.BUTTON_D)
def button_d(button, pressed):
    messageD = "Button D was pressed"
    print(messageD)
    buttonshim.set_pixel(255, 0, 255)
    
    inky_display = InkyPHAT("red")
    inky_display.set_border(inky_display.WHITE)
    
    img = Image.new("P", (inky_display.WIDTH, inky_display.HEIGHT))
    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype(FredokaOne, 15)
    w, h = font.getsize(messageD)
    x = (inky_display.WIDTH / 2) - (w / 2)
    y = (inky_display.HEIGHT / 2) - (h / 2)
    draw.text((x, y), messageD, inky_display.BLACK, font)
    inky_display.set_image(img)
    inky_display.show()

Thank you all.

I’m no python expert, far from it, but. I see you defining your def’s, but where are you calling them up? Shouldn’t there be a “while true” (repeating) section where the button presses are detected and acted on?

Technically they get called every time i press the button… I’ll try adding a while true though. Thanks

@alphanumeric is right, in the example scripts this is handled by having this at the bottom of the script:

signal.pause() # Stop script from immediately exiting

So Python is reading all of your functions, reaches the end of the script and then quits.