from gpiozero import TrafficHat from signal import pause th = TrafficHat() th.button.when_pressed = th.on th.button.when_released = th.off pause()