Hello,
Total Python noob here -- I'm an experienced Node/JS dev but I don't know Python very well yet, so forgive me if this is a dumb question.
I've got a Pimoroni Display Hat Mini and I'm using the st7789 library to drive it, as Pimoroni hasn't updated their own driver library for the Pi 5. Since that library doesn't deal with the onboard buttons on the hat, I'm trying to use gpiozero to access them. It's working, but for the life of me I can't figure out how to turn the backlight on or off when a button is pressed.
I've got the button defined and a global backlightTrue variable, and there's a while True: loop that scrolls text across the screen. I tried this:but nothing happens when I press the button. And if I put this within my while True loop:
It works... but of course it flickers, because it's checking it over and over.
I tried putting the "button_a.when_pressed = toggle_backlight" inside the while loop and no go. I tried setting the bounce_time of the button to 1 second, nothing.
I've tried searching but I can't seem to find any answers. I know this is probably a dumb question, but I'm just really not sure how this works -- it's been decades since I wrote much code in anything but Node, which of course is event-driven rather than doing while loops.
Can anyone explain to me what I'm doing wrong here?
Total Python noob here -- I'm an experienced Node/JS dev but I don't know Python very well yet, so forgive me if this is a dumb question.
I've got a Pimoroni Display Hat Mini and I'm using the st7789 library to drive it, as Pimoroni hasn't updated their own driver library for the Pi 5. Since that library doesn't deal with the onboard buttons on the hat, I'm trying to use gpiozero to access them. It's working, but for the life of me I can't figure out how to turn the backlight on or off when a button is pressed.
I've got the button defined and a global backlightTrue variable, and there's a while True: loop that scrolls text across the screen. I tried this:
Code:
def toggle_backlight(): global disp global backlightOn backlightOn = not backlightOn print(backlightOn and "Turned backlight ON" or "Turned backlight OFF") disp.set_backlight(backlightOn) button_a.when_pressed = toggle_backlightCode:
if button_a.is_pressed: toggle_backlight()I tried putting the "button_a.when_pressed = toggle_backlight" inside the while loop and no go. I tried setting the bounce_time of the button to 1 second, nothing.
I've tried searching but I can't seem to find any answers. I know this is probably a dumb question, but I'm just really not sure how this works -- it's been decades since I wrote much code in anything but Node, which of course is event-driven rather than doing while loops.
Can anyone explain to me what I'm doing wrong here?
Statistics: Posted by jzellis — Thu May 29, 2025 1:18 am — Replies 0 — Views 39