kivy:在幻灯片转换开始时播放声音,按下按钮后停止播放

2024-06-26 17:03:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的scren构建一个应用程序,以从人们那里获取反馈,我想发出通知,直到有人来按开始按钮。公告以mp3文件形式发布。 我想在屏幕0出现时播放声音,但想在按下开始按钮时停止声音。如何实现,请帮助

这是main.py文件

main.py

from kivy.app import App # Use of fields and methods of Kivy
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition

class Screen0(Screen):
    pass

class Screen1(Screen):
    pass

class Screen2(Screen):
    pass

class Screen3(Screen):
    pass

class Screen4(Screen):
    pass

class Screen5(Screen):
    def on_start(self):
        Clock.schedule_once(self.change_screen, 0.5)

    def change_screen(self, *kwargs):
         app.root.transition = SlideTransition(direction="left")
         app.root.current = "screen2"
         print("bye BYE")
class ScreenManagement(ScreenManager):
    pass

presentation = Builder.load_file("style.kv")

class MyApp(App):
    def build(self):
        return presentation

myApp = MyApp()
myApp.run()

这是kv文件

1.5千伏

#:import SlideTransition kivy.uix.screenmanager.SlideTransition
ScreenManagement:
    transition: SlideTransition()
    Screen0:
    Screen1:
    Screen2:
    Screen3:
    Screen4:
    Screen5:
<Screen0>:
    name: "screen0"
    FloatLayout:
        Label:
            text: "PLEASE GIVE US YOUR \n VALUABLE FEEDBACK!"
            pos_hint: {"top": 1.2}
            font_size: 48
            color: 1,1,1,1  
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen2"
            size_hint: 0.2, 0.8/3
            pos_hint: {"center_x":0.5, "center_y":.4} 
            text: "START"

            background_color: (0.0, 1.0, 0, 1.0)
            font_size: 24
<Screen1>:
    name: "screen1"
    FloatLayout:
        Label:
            text: "WHICH IS YOUR FAVORITE SWEET?"
            pos_hint: {"top": 1.2}
            font_size: 48
            color: 1,1,0,1  
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen2"
            size_hint: 0.2, 0.8/3
            pos_hint: {"center_x":0.2, "center_y":.4} 
            text: "HALDIRAMS \n KAJU KATHALI"

            background_color: (0.0, 1.0, 1.0, 1.0)
            font_size: 18
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen2"
            size_hint: 0.2, 0.8/3
            pos_hint: {"center_x":0.4, "center_y":.4}
            text: "HALDIRAMS \n SONPAPDI"
            background_color: (1.0, 0.0, 0.0, 1.0)
            font_size: 18
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen2"
            size_hint: 0.2, 0.8/3
            pos_hint: {"center_x":0.6, "center_y":.4}
            text: "HALDIRAMS \n BADAM HALWA"
            background_color: (1.0, 1.0, 0.0, 1.0)
            font_size: 18
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen2"
            size_hint: 0.2, 0.8/3
            pos_hint: {"center_x":0.8, "center_y":.4}
            text: "HALDIRAMS \n RASGULLA"
            background_color: (0.8, 0.3, 0.4, 1.0)
            font_size: 18
<Screen2>:
    name: "screen2"
    FloatLayout:
        Label:
            text: "AFTER HOW MUCH TIME \n YOU GOT THE ORDER?"
            pos_hint: {"top": 1.2}
            font_size: 48
            color: 1,1,0,1
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen3"
            size_hint: 0.2, 0.8/3
            pos_hint: {"center_x":0.2, "center_y":.4}
            text: "Immediatly"

            background_color: (0.0, 0.5, 1.0, 1.0)
            font_size: 18
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen3"
            size_hint: 0.2, 0.8/3
            pos_hint: {"center_x":0.4, "center_y":.4}
            text: "In 5 to\n 15 Min"
            background_color: (1.0, 0.4, 0.8, 1.0)
            font_size: 18
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen3"
            size_hint: 0.2, 0.8/3
            pos_hint: {"center_x":0.6, "center_y":.4}
            text: "In 15 to\n 30 Min"
            background_color: (1.0, 0.8, 0.4, 1.0)
            font_size: 18
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen3"
            size_hint: 0.2, 0.8/3
            pos_hint: {"center_x":0.8, "center_y":.4}
            text: "45 Min or More"
            background_color: (1.0, 0.0, 1.0, 1.0)
            font_size: 18
<Screen3>:
    name: "screen3"
    FloatLayout:
        Label:
            text: "HOW WAS THE BEHAVIOUR \n OF MANAGER/SERVENT?"
            pos_hint: {"top": 1.2}
            font_size: 48
            color: 1,1,0,1
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen4"
            size_hint: 0.2, 0.8/3
            pos_hint: {"center_x":0.2, "center_y":.4}
            text: "VERY GOOD"

            background_color: (0.0, 0.5, 1.0, 1.0)
            font_size: 18
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen4"
            size_hint: 0.2, 0.8/3
            pos_hint: {"center_x":0.4, "center_y":.4}
            text: "GOOD"
            background_color: (1.0, 0.8, 0.4, 1.0)
            font_size: 18
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen4"
            size_hint: 0.2, 0.8/3
            pos_hint: {"center_x":0.6, "center_y":.4}
            text: "NOT SO \nGOOD"
            background_color: (1.0, 0.4, 0.8, 1.0)
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen4"
            size_hint: 0.2, 0.8/3
            pos_hint: {"center_x":0.8, "center_y":.4}
            text: "VERY BAD"
            background_color: (1.0, 1.0, 0.0, 1.0)
<Screen4>:
    name: "screen4"
    FloatLayout:
        Label:
            text: "WILL YOU AGAIN SHOP IN \n OUR STORE?"
            pos_hint: {"top": 1.2}
            font_size: 48
            color: 1,1,0,1
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen5"
            size_hint: 0.2, 0.8/3
            pos_hint: {"center_x":0.2, "center_y":.4}
            text: "DEFINETLY"

            background_color: (0.5, 1, 1.0, 1.0)
            font_size: 18
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen5"
            size_hint: 0.2, 0.8/3
            pos_hint: {"center_x":0.4, "center_y":.4}
            text: "YES"
            background_color: (1.0, 0.0, 0.0, 1.0)
            font_size: 18
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen5"
            size_hint: 0.2, 0.8/3
            pos_hint: {"center_x":0.6, "center_y":.4}
            text: "NO"
            background_color: (1.0, 1.0, 0.0, 1.0)
            font_size: 18
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen5"
            size_hint: 0.2, 0.8/3
            pos_hint: {"center_x":0.8, "center_y":.4}
            text: "CANT SAY"
            background_color: (1.0, 0.0, 1.0, 1.0)
            font_size: 18
<Screen5>:
    name: "screen5"
    FloatLayout:
        Label:
            text: "!!!THANK YOU FOR YOUR\n FEEDBACK+ \n HAVE A NICE DAY!!!"
            pos_hint: {"top":1.2}
            font_size: 48
            color: 1,1,1,1

请分享一些建议


Tags: textposappsizeonrootcurrentleft
1条回答
网友
1楼 · 发布于 2024-06-26 17:03:36

MyApp中添加处理声音的代码:

class MyApp(App):
    def build(self):
        self.sound = SoundLoader.load('test.mp3')
        self.sound.loop = True
        self.sound.play()
        return presentation

    def stop_sound(self):
        self.sound.stop()

然后,在“kv”中,将Start{}规则更改为:

    Button:
        on_release:
            app.root.transition = SlideTransition(direction="left")
            app.root.current = "screen2"
            app.stop_sound()
        size_hint: 0.2, 0.8/3
        pos_hint: {"center_x":0.5, "center_y":.4} 
        text: "START"

相关问题 更多 >