How do I create a guizero pushbutton that acts when pressed and stops when released

17 Views Asked by At

I have dummied this application down to a single button on a form. When I press this button it should print "Clicked" repeatedly until I release it at which point it should print "Button Released" once. So my output should be 10's or 100's of lines of "Clicked" with a single final line of "Button Released". Ultimately the code would trigger a raspberry pi GPIO to "jog" or actuate a linear actuator only when the button is pressed.

I've tried so many things, but I'll share the last approach I was trying.

`from guizero import App, PushButton from time import sleep

app = App(layout="grid")

def playpen(): global clicked clicked = 1 while clicked == 1: print("Clicked = ", clicked)

print("Button Released")

def playpen2():
clicked = 0

buttonJogCutterUp1 = PushButton(app, grid = [0,1])

buttonJogCutterUp1.when_left_button_pressed = playpen buttonJogCutterUp1.when_left_button_released = playpen2 app.display()`

What happens is an infinite loop. The "when_left_button_released" event is not changing the value of clicked, so the while loop remains true.

I've also tried repeat loops on the button, and this works to the point that I can see the "release" event, but then the "repeat" function continues and I'm unable to interrupt it. So frustrated.

0

There are 0 best solutions below