Python Tkinter after loop not working fast enough

Posted by user2658538 on Stack Overflow See other posts from Stack Overflow or by user2658538
Published on 2014-06-07T03:17:52Z Indexed on 2014/06/07 3:25 UTC
Read the original article Hit count: 190

Filed under:
|
|

I am making a simple metronome where it plays a tick sound every few milliseconds depending on the bpm and plays the sound using the winsound module. I use tkinter because there will be a gui component later but for now the metronome code is working, it plays the sound at a constant rate, but even though I set the after loop to play the sound every few milliseconds, it waits longer and the beat is slower than it should be. Is it a problem with the code or a problem with the way I calculate the time?

Thanks.

Here is my code.

from Tkinter import *
import winsound,time,threading
root=Tk()
c=Canvas(root)
c.pack()
class metronome():
    def __init__(self,root,canvas,tempo=100):
        self.root=root
        self.root.bind("<1>",self.stop)
        self.c=canvas
        self.thread=threading.Thread(target=self.play)
        self.thread.daemon=True
        self.pause=False
        self.tempo=tempo/60.0
        self.tempo=1.0/self.tempo
        self.tempo*=1000
    def play(self):
        winsound.PlaySound("tick.wav",winsound.SND_FILENAME)
        self.sound=self.c.after(int(self.tempo),self.play)
    def stop(self,e):
        self.c.after_cancel(self.sound)
beat=metronome(root,c,120)
beat.thread.start()
root.mainloop()

© Stack Overflow or respective owner

Related posts about python

Related posts about audio