i am programming a pythonscript with guizero. there is a PushButton and its command runs a function. This function ist build after a PomodoraTimer Tutorial i found on youtube. As far, it works, but i want it to stop, after the alarm is running. The Code is the following:
#UserInterface, GPIO-Steuerung und Timer-Steuerung importieren
from guizero import App, Text, TextBox, PushButton, Box, Window
import RPi.GPIO as GPIO
import time
import datetime
#this is what gets triggered by the Pushbutton
def startTimer():
global timer
if (set_time_input.value == "" and set_time_input_min.value == ""):
currentTimeLeft = "3" #60 #set timer to 1 minuit if textfield empty
else:
if (set_time_input.value != "" and set_time_input_min.value == ""):
currentTimeLeft = int(set_time_input.value)
else:
currentTimeLeft = int(set_time_input_min.value) * 60
#Countdownanzeige
timer = Text(timer_box, text="\r"+str(currentTimeLeft), size=30, color="red")
timerbottom = Text(timer_box, text="\n Sekunden.")
timer.repeat(1000, reduceTime)
def reduceTime():
global timer
if int(timer.value) > 0:
timer.value = int(timer.value) -1
else:
start_alarm_1() #this triggers a GPIO on my Raspi for 3 seconds
#exit()
#####this is just the rest of the app if ou want to build it and test it:
#GPIO Steuerung initialisieren
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.cleanup()
#Start Alarm -> i've got a remote alarm that works this way ;)
def start_alarm_1():
GPIO.setup(20, GPIO.OUT)
GPIO.output(20, GPIO.LOW)
time.sleep(3)
GPIO.output(20, GPIO.HIGH)
#App-Fenster initialisieren
app = App(title="OSCE Timer", width=800, height=400)
#textfields to set timer time
textfield_msg = Text(app, text="Timer in seconds:")
set_time_input = TextBox(app)
textfield_msg = Text(app, text="OR timer in Minutes:")
set_time_input_min = TextBox(app)
run_timer_button = PushButton(app, command=startTimer, text="start Countdown")
#timer window in app
timerTitle = Text(app, text="Time:\n")
#Appwindow
app.display()
My problem is: if in the reduceTime() the timer.value gets "0", then the alarm starts over and over again. this is because the timer.repeat() function is triggering the reduceTime() function over and over again - for ever. Now i had multiple ideas: build a while-loop around the repeat() function, add return -1 statements to different places, but none of it worked. I would be so happy if anyone could help me! (the problem with the while statment: it doesnt display my timer anymore because the function is stucked in the while loop obv.)
Damn, after hours of trying i posted this - and few seconds later I found a solution!
I had to add the following line insted of the '#exit()' statement i also worked with:
time.cancel(reduceTime)