-5

I'm having problems with a countdown clock that I was making in Python for a Raspberry Pi. I need to have a countdown clock that counts down from 60 minutes. When time runs out it should display a red text "GAME OVER".

I've already made one using TKinter and a for loop for the actual timer but I couldn't find any way to stop the for loop. I gave up on it.

Is there anyone nice enough to maybe write the actual timer and timer stopping part? I'm good enough at python and TKinter to do everything else that I need.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Daniel Bether
  • 11
  • 1
  • 3
  • 3
    We're not going to write your code for you. If you have a specific question, feel free to ask one. – Morgan Thrapp Mar 09 '16 at 18:19
  • 2
    Your question is difficult to answer as it doesn't contain your code. You'll need to post your code so that people can look at it and point out where the problem is. It's preferred that you create a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Also see [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask). You can use the "edit" button to update your question. Good luck! – Martin Tournoij Mar 09 '16 at 18:19
  • 1
    I'd suggest adding `yield` statements into the loop that handles the timer and have occasional callbacks to `next()` that continues the loop. Won't post an answer just writing code for you and you don't provide any code to work off of. – Tadhg McDonald-Jensen Mar 09 '16 at 18:20

1 Answers1

0

I'd recommend using generators to handle your for loop and will provide a minimal example but on StackOverflow no one is going to "write the actual timer and timer stopping part" (see What topics can I ask here)

Note this is an example I had before this question was posted and thought it would be helpful to you.

import tkinter as tk

def run_timer():
    for time in range(60):
        label["text"] = time #update GUI here
        yield #wait until next() is called on generator

root = tk.Tk()

label = tk.Label()
label.grid()

gen = run_timer() #start generator
def update_timer():
    try:
        next(gen)
    except StopIteration:
        pass #don't call root.after since the generator is finished
    else:
        root.after(1000,update_timer) #1000 ms, 1 second so it actually does a minute instead of an hour

update_timer() #update first time

root.mainloop()

you will still need to figure out for yourself how to implement after_cancel() to stop it and the red "GAME OVER" text.

Community
  • 1
  • 1
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59