简体   繁体   English

如何在.kv文件上绑定按钮以使其播放声音?

[英]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. 您好,StackOverflow社区,我想寻求一些帮助,就像在问我进行长期研究之前,没有发现任何帮助。

I have a school project that I decided to code with Python using Kivy for cross-platform. 我有一个学校项目,我决定使用Kivy跨平台使用Python进行编码。 My project is about a SoundBox, to simplify I need to first create buttons and bind them to play various sounds. 我的项目是有关SoundBox的,为简化起见,我需要先创建按钮并将其绑定以播放各种声音。 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. 在纯python代码(没有.kv文件)上,我学习了如何绑定按钮以使其发出声音,因此我决定进入下一级别,即“屏幕管理”部分。 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. 我有点了解到现在可以使用.kv文件使其变得简单,但是我仍然坚持如何使用.kv文件绑定按钮。

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. 我尝试了一些方法,但是总是在控制台上出现错误,但是(暂时还不很重要),我的Fade Transition不起作用。 Your help is highly appreciated, thanks in advance. 在此先感谢您的帮助。

.py: 的.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: .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. 问题是您的sound超出了可以在kv文件中使用的范围。 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) 然后以更有效的方式收集参数-这样,您就可以在kv和python中使用它,并且将收集其他args(不会引发错误)

    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. 然后在kv中,您必须确保ScreenManager仅接收适当的小部件,即仅Screen 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: 要实际播放声音(运行方法),您可以从在App实例中定义声音的地方调用它:

<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: 您可以将其导入到kv文件中:

#: import sound __main__.sound

Button:
    on_release: sound()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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