gpt4 book ai didi

python - kivy:在幻灯片过渡开始时播放声音,并在按下按钮时停止

转载 作者:行者123 更新时间:2023-12-02 22:30:52 26 4
gpt4 key购买 nike

我正在为自己的镜头建立一个从别人那里获取反馈的应用程序,并且我想通告大家直到有人来并按开始按钮。该公告位于mp3文件中。
我想在出现screen0时播放声音,但想在按下开始按钮时停止播放声音。如何实现这一点请帮助。
这是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文件
style.kv
#: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
请分享一些建议

最佳答案

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规则更改为:
    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"

关于python - kivy:在幻灯片过渡开始时播放声音,并在按下按钮时停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58662831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com