Trying to build a metronome in kivy - Event loop management
since I haven't found anything here, I try and put my Question.
I'm trying to build a simple metronome in kivy. I basically took the Audio example which was provided with the installation und wanted to add a metronome functionality.
class AudioButton(ToggleButton): filename = StringProperty(None) sound = ObjectProperty(None) def on_filename(self, instance, value): # the first time that the filename is set, we are loading the sample if self.sound is None: self.sound = SoundLoader.load(value) def on_press(self): # stop the sound if it's currently playing if self.sound.status != 'stop': self.sound.stop() self.sound.play()
As you can see, I changed the class from Button to ToggleButton.
I tried to put a while loop with the self.sound.play(), but that results in infinity, so basically I'm looking for a way to break out of the loop, if I press the Button again.
I didn't really understand the Event loop management from the documentation, which I think should be the answer, but I never used an event loop before. It would be great if someone could provide some example code for such a situation.
The place that you want to start looking is in the Kivy docs discussing clock events. In event-driven systems like this any kind of while loop takes the system to its knees. Instead, when you want something to happen periodically, you ask the system to set up a timer and tell it the function that you'd like to have called every time that timer elapses.
In your case, you'd probably just play that single metronome tick and be done until the next time the timer elapses.