-2

I'm trying to make a generic countdown timer. If was GUI based, using tk.after would do the magic, but it is not.

During given time interval, should it returns 1 when countdown in progress and 0 when it is off. This counter will run in background inside other classes (that may be GUI class).

for example, let TimeOut be that countdown timer:

a = TimeOut(days=0,seconds=100) # a runs in background for 100 seconds.
a.status() # during countdown time - `status` returns `1`.

Is there an elegant way to create such class?

guyd
  • 693
  • 2
  • 14
  • 32
  • How do not expect `class TimeOut` to not use `tkinter` if it's derived from the `tkinter.Frame` class? It also has a number of other dependencies. Please define exactly what methods the independent class is to have. – martineau Oct 26 '17 at 20:28
  • @martineau - I do not need any `tkinter` dependncy it was only for example. you can see at upper part of my question what is needed: time interval, and output, 0 or 1 – guyd Oct 26 '17 at 20:32
  • You could make a function with time.sleep(seconds). – Nae Oct 26 '17 at 20:37
  • @Nae as I wrote- it should run in backgroud, using sleep, will pause entire code for that time – guyd Oct 26 '17 at 20:38
  • 1
    Guy: You shouldn't use `sleep()` in a tkinter program because it will hang the GUI's `mainloop()` at it won't respond to user input for the duration. – martineau Oct 26 '17 at 22:00

1 Answers1

2

This has nothing to do with tkinter.

Use a threading.Timer.

>>> from threading import Timer
>>> t = Timer(10, lambda: None)
>>> t.start()
>>> t.is_alive() # call before 10 seconds is up
True
>>> t.is_alive() # call after 10 seconds is up
False
Novel
  • 13,406
  • 2
  • 25
  • 41