简体   繁体   中英

How can I bind a button on the .kv file to make it play a sound?

Hello StackOverflow community, I would like to ask for some help as before asking I did a long research but found nothing to help me out.

I have a school project that I decided to code with Python using Kivy for cross-platform. My project is about a SoundBox, to simplify I need to first create buttons and bind them to play various sounds. On pure python code (without a .kv file), I learned how to bind a button to make it play a sound, so I decided to reach the next level that is the Screen Management part. I kind of learned that to using now a .kv file to make it simple but I'm stuck on how to bind a button using .kv file.

I tried out some stuff but always ended up with errors on the console, also (but it's not really important for now), my Fade Transition doesn't work. Your help is highly appreciated, thanks in advance.

.py:

from kivy.app import App
from kivy.uix.button import Button
from kivy.core.audio import SoundLoader
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition

sound = SoundLoader.load('Sunset_Lover.ogg')
sm = ScreenManager()

class ScreenManager(ScreenManager):
    pass

class Menu(Screen):
    pass

class Genre(Screen):
    pass

class TestApp(App):
    def build(self):
        sm.add_widget(Menu(name='menu'))
        sm.add_widget(Genre(name='genre'))
        return sm
    def son(self, instance):
        if sound:
            sound.play()

if __name__ == "__main__":
    TestApp().run()

.kv:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

<ScreenManager>:
    FadeTransition:
    Menu:
    Genre:
<Menu>:
    BoxLayout:
        Button:
            text: "Commencer"
            size_hint: 1, 1
            pos_hint: {'x': 0.3, 'y':0.3}
            on_press: root.manager.current = 'genre'
<Genre>:
    BoxLayout:
        Button:
            text: "Exemple1"
            size_hint: 0.2, 0.2
            pos_hint: {'x': 0.2, 'y':0.2}
            on_press: root.son()

The problem is that you have sound out of any scope you could use in kv file. First, move it somewhere, where you can access it:

class TestApp(App):
    def build(self):
        self.sound = SoundLoader.load('file')
        sm = ScreenManager()
        sm.add_widget(Menu(name='menu'))
        sm.add_widget(Genre(name='genre'))
        return sm

Then collect the arguments in more efficient way - this way you can use it both in kv and python and the additional args will be collected (won't throw an error)

    def son(self, *args):
        if self.sound:
            self.sound.play()

Then in kv you have to make sure the ScreenManager recieves only the appropriate widgets ie Screen s only. To get the transition working, you have to add it to a variable it's used from :

<ScreenManager>:
    transition: FadeTransition()
    Menu:
    Genre:

And to actually play the sound (run the method) you can call it from the place you define it in ie from App instance:

<Genre>:
    BoxLayout:
        Button:
            text: "Exemple1"
            size_hint: 0.2, 0.2
            pos_hint: {'x': 0.2, 'y':0.2}
            on_press: app.son()  # here

You can import it to the kv file:

#: import sound __main__.sound

Button:
    on_release: sound()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM