0

I have been helped to make a timer where if I press the space key, it restarts. I would like to edit this code so when I press the return/enter button, it pauses the timer, regardless of where the timer is (restarted, at 0, or in the middle). Of course, I don't want it to affect the rest of the code. Here is my code:

from turtle import Screen, Turtle, bgcolor
bgcolor('dodgerblue')
FONT = ("Arial", 60, "normal")

strings = input("Please enter the time: ").strip().split(' ')

time = [60 ** (len(strings) - index - 1) * int(unit) for index, unit in enumerate(strings)]

seconds = -1
ticking = False
def delSec(string):
    if len(string)==1:
        return "0"+string
    else:
        return string
def tick():
    global seconds, ticking

    turtle.clear()

    if seconds < 0:
        turtle.write("TIMER DONE", align='center', font=FONT)
        ticking = False
    else:
        turtle.write(delSec(str(seconds//3600))+":"+delSec(str((seconds%3600)//60))+":"+delSec(str((seconds%60)//1)), align='center', font=FONT)
        seconds -= 1
        screen.ontimer(tick, 1000)
key = "space"
def reset():
    global seconds, ticking, key

    screen.onkey(None, key)  # Disable event handler inside handler

    seconds = sum(time)

    if not ticking:
        ticking = True
        tick()

    screen.onkey(reset, key)  # Reenable event handler

screen = Screen()

turtle = Turtle()
turtle.hideturtle()
turtle.color('white')

reset()

screen.listen()
screen.mainloop()

I would appreciate any help, thanks in advance!


EDIT: I want it so that if the timer is paused, I can still reset it.

1 Answer 1

1

I use key Return to run function which change paused

def pause():
    global paused

    paused = not paused

and use this paused to control tick()

def tick():
    global seconds, ticking

    if not paused:
        turtle.clear()

        if seconds < 0:
            turtle.write("TIMER DONE", align='center', font=FONT)
            ticking = False
            return
        else:
            turtle.write(delSec(str(seconds//3600))+":"+delSec(str((seconds%3600)//60))+":"+delSec(str((seconds%60)//1)), align='center', font=FONT)
            seconds -= 1

    screen.ontimer(tick, 1000)

tick() has to run ontimer() even if it paused


from turtle import Screen, Turtle, bgcolor

# --- functions ---

def delSec(string):
    if len(string) == 1:
        return "0" + string
    else:
        return string

def tick():
    global seconds, ticking

    if not paused:
        turtle.clear()

        if seconds < 0:
            turtle.write("TIMER DONE", align='center', font=FONT)
            ticking = False
            return
        else:
            turtle.write(delSec(str(seconds//3600))+":"+delSec(str((seconds%3600)//60))+":"+delSec(str((seconds%60)//1)), align='center', font=FONT)
            seconds -= 1

    screen.ontimer(tick, 1000)

def reset():
    global seconds, ticking, key,
    #global paused

    screen.onkey(None, key)  # Disable event handler inside handler
    screen.onkey(None, key_pause)  # Disable event handler inside handler

    seconds = sum(time)

    if not ticking:
        ticking = True
        tick()

    #paused = False

    screen.onkey(reset, key)  # Reenable event handler
    screen.onkey(pause, key_pause)  # Reenable event handler

def pause():
    global paused

    paused = not paused

# --- main ---

bgcolor('dodgerblue')
FONT = ("Arial", 60, "normal")

#strings = input("Please enter the time: ").strip().split(' ')
strings = '10'.strip().split(' ')

time = [60 ** (len(strings) - index - 1) * int(unit) for index, unit in enumerate(strings)]

seconds = -1
ticking = False
paused = False
key = "space"
key_pause = "Return"

screen = Screen()

turtle = Turtle()
turtle.hideturtle()
turtle.color('white')

reset()

screen.listen()
screen.mainloop()

To reset text even when it paused I pause only seconds -= 1 - so it redraw time in every loop - even if it paused - so reset can change value.

def tick():
    global seconds, ticking


    turtle.clear()

    if seconds < 0:
        turtle.write("TIMER DONE", align='center', font=FONT)
        ticking = False
        return
    else:
        turtle.write(delSec(str(seconds//3600))+":"+delSec(str((seconds%3600)//60))+":"+delSec(str((seconds%60)//1)), align='center', font=FONT)
        if not paused:
              seconds -= 1

    screen.ontimer(tick, 1000)
Sign up to request clarification or add additional context in comments.

4 Comments

when you press 'space' and later unpause it then you will see it is reseted.
to update reseted clokc automatically when it paused it would need some work.
No that's fine. Thank you!
I moved if not paused: and now it pauses only seconds -= 1 and it redraws text in every loop so reset can change its value - but now it blink even when it paused.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.