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.

Answers


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.


Need Your Help

ZF2 router configuration for handling multiple subdomains

routing zend-framework2

I'm working on a zf2 project and i need to configure some of my modules to work on different subdomains following scenario:

Where should I store machine-wide application settings for non-roaming users?

c# .net windows application-data roaming-profile

I have a WPF application that must run for all users of a machine with the same settings. The settings must be read/write. I have previously been storing user configuration settings in

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.