1

I'm wondering how I can play a .wav file after some time has passed without using the sleep function. Essentially, I was wondering if there is a way to keep track of time in Python, so that, after say 15 seconds has elapsed, I can play a sound without pausing my code.

# checks if I should play the sound or not and, sets the variable

def Tyler(self):
    if self.started == 1:
        if self.isTaiwan == 0:
            if self.myListNames[self.current_player_id / 3].lower() == "tyler":
                self.isTyler = 1
            else:
                self.isTyler = 0

self.Tyler()

if self.isTyler == 1:
    time.sleep(6)
    winsound.PlaySound("tyler.wav", winsound.SND_ASYNC)

# This is where I would want to check to see 
# if some time has passed and the conditions haven't changed.
nbro
  • 15,395
  • 32
  • 113
  • 196
Nick Folz
  • 93
  • 1
  • 7
  • 4
    http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.after-method ? – Eric Levieil Jun 26 '15 at 21:42
  • Thank you that was exactly what I was looking for! – Nick Folz Jun 26 '15 at 21:49
  • @EricLevieil: Convert your comment to an answer so we can vote it up! ;) – Ethan Furman Jun 26 '15 at 23:47
  • @EthanFurman I am still quite new here so feel free to correct me. To me, this question could be seen as a duplicate of http://stackoverflow.com/questions/459083/how-do-you-run-your-own-code-alongside-tkinters-event-loop and some people says you're not supposed to answer duplicates. – Eric Levieil Jun 27 '15 at 10:55
  • Go ahead and add your answer. Sometimes different questions have the same answer, and this is one of those cases. – Ethan Furman Jun 27 '15 at 18:18

1 Answers1

0
from time import time
def delay(secs):
    init_time = time()
    while time() < init_time+secs: pass

This won't use sleep but it is similar. Still uses time module

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Lucas Urban
  • 627
  • 4
  • 15