Button 如何使用Kivy在加载动画时放置按钮?

Button 如何使用Kivy在加载动画时放置按钮?,button,kivy,python-3.7,Button,Kivy,Python 3.7,我对Kivy真的很陌生,我正在尝试制作我的第一个应用程序,但我真的不知道如何使用元素和类 我正试图按下一个按钮来停止声音,但它只会停止动画 这是代码,我想我没有正确编码!:( 从kivy.app导入应用 从kivy.lang导入生成器 从kivy.uix.floatlayout导入floatlayout 从kivy.animation导入动画 从kivy.properties导入NumericProperty 从kivy.core.audio导入SoundLoader 从kivy.uix.but

我对Kivy真的很陌生,我正在尝试制作我的第一个应用程序,但我真的不知道如何使用元素和类

我正试图按下一个按钮来停止声音,但它只会停止动画

这是代码,我想我没有正确编码!:(

从kivy.app导入应用
从kivy.lang导入生成器
从kivy.uix.floatlayout导入floatlayout
从kivy.animation导入动画
从kivy.properties导入NumericProperty
从kivy.core.audio导入SoundLoader
从kivy.uix.button导入按钮
从functools导入部分
从kivy.uix.boxlayout导入boxlayout
Builder.load_字符串(“”)
: 
在以下情况之前:
推矩阵
轮换:
角度:root.angle
轴:0,0,1
来源:根.center
在下列情况之后:
流行音乐
图片:
id:img_anim
来源:“logo.png”
大小提示:0,0
位置提示:{'center_x':0.5,'center_y':0.5}
''')
类应用程序容器(浮动布局):
角度=数值属性(0)
定义初始(自我,**kwargs):
#动画
超级(应用程序容器,自我)。\uuuuu初始化(**kwargs)
动画=动画(角度=360,持续时间=2)
动画2=动画(大小提示=(2,2),持续时间=2)
动画开始(自我)
anim2.start(self.ids[“img\u anim”])
#儿子
self.sound=SoundLoader.load('zik.wav'))
self.sound.loop=True
self.sound.play()
#布通齐克
btn=按钮(text=“推我!”)
self.add_小部件(btn)
绑定(on_press=partial(self.foo,btn))
def foo(self、instance、*args):
self.sound.volume=0
类TestApp(应用程序):
def生成(自):
返回App_容器()
如果名称=“\uuuuu main\uuuuuuuu”:
app=TestApp()
app.run()

为了使
按钮不旋转,它不能处于旋转的
布局中。为此,您可以在
应用程序容器中添加另一个
浮动布局
,并且只旋转该
浮动布局
。以下代码修改可以:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.animation import Animation
from kivy.core.audio import SoundLoader

Builder.load_string('''                               
<App_container>:
    FloatLayout:
        # move the angle property into this FloatLayout
        angle: 0.0
        id: rotate_this
        canvas.before:
            PushMatrix
            Rotate:
                angle: self.angle
                axis: 0, 0, 1
                origin: root.center
        canvas.after:
            PopMatrix

        Image:
            id: img_anim
            source: 'logo.png'
            size_hint: 0,0
            pos_hint: {'center_x': 0.5, 'center_y': 0.5}
    Button:
        text: 'Push Me'
        on_press: root.foo(self)
        size_hint: 0.1,0.1
        pos_hint: {'center_x':0.5, 'center_y':0.5}
''')


class App_container(FloatLayout):
    def __init__(self, **kwargs):
        #Anim
        super(App_container, self).__init__(**kwargs)
        anim = Animation(angle = 360, duration=2)
        anim2 = Animation(size_hint=(2,2), duration=2)

        # rotate the FloatLayout with id "rotate_this"
        anim.start(self.ids["rotate_this"])

        # animate the "img_anim"
        anim2.start(self.ids["img_anim"])
        #Son
        self.sound = SoundLoader.load('zik.wav')
        self.sound.loop = True
        self.sound.play()

    def foo(self, instance, *args):
        self.sound.volume=0


class TestApp(App):

    def build(self):
        return App_container()


if __name__ == "__main__":
    app = TestApp()
    app.run()
从kivy.app导入应用
从kivy.lang导入生成器
从kivy.uix.floatlayout导入floatlayout
从kivy.animation导入动画
从kivy.core.audio导入SoundLoader
Builder.load_字符串(“”)
:
浮动布局:
#将角度特性移动到此布局中
角度:0.0
id:请旋转这个
在以下情况之前:
推矩阵
轮换:
角度:self.angle
轴:0,0,1
来源:根.center
在下列情况之后:
流行音乐
图片:
id:img_anim
来源:“logo.png”
大小提示:0,0
位置提示:{'center_x':0.5,'center_y':0.5}
按钮:
文字:“推我”
按:root.foo(self)
大小提示:0.1,0.1
位置提示:{'center_x':0.5,'center_y':0.5}
''')
类应用程序容器(浮动布局):
定义初始(自我,**kwargs):
#动画
超级(应用程序容器,自我)。\uuuuu初始化(**kwargs)
动画=动画(角度=360,持续时间=2)
动画2=动画(大小提示=(2,2),持续时间=2)
#旋转id为“rotate_this”的浮动布局
动画开始(self.ids[“旋转此”])
#为“img_anim”制作动画
anim2.start(self.ids[“img\u anim”])
#儿子
self.sound=SoundLoader.load('zik.wav'))
self.sound.loop=True
self.sound.play()
def foo(self、instance、*args):
self.sound.volume=0
类TestApp(应用程序):
def生成(自):
返回App_容器()
如果名称=“\uuuuu main\uuuuuuuu”:
app=TestApp()
app.run()

因此,
FloatLayout
旋转,但
按钮不旋转,因为它不在旋转的
FloatLayout

中,
build()中不能有两个
return
语句
method。我修改了我的代码,但该按钮仍将具有与动画相同的行为。您修改的代码对我有效。我已更改了音频和图像文件,但没有其他更改。按下该按钮只会使音频静音,动画将继续。我正在Ubuntu 18.04上使用Kivy v1.11.1和Python v3.6.9。您在使用什么?Python 3.7.7和kivy 1.11.1 win7按钮不会为您旋转?我想要的是我的图像旋转(它是应用程序徽标)和一个静音按钮(因为它是循环的)是的,按钮会旋转,但按下按钮只会停止音频。这不是你想要的吗?非常感谢!我对Kivy一窍不通,这正是我所期待的!我开始更好地理解它的工作原理了!
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.animation import Animation
from kivy.core.audio import SoundLoader

Builder.load_string('''                               
<App_container>:
    FloatLayout:
        # move the angle property into this FloatLayout
        angle: 0.0
        id: rotate_this
        canvas.before:
            PushMatrix
            Rotate:
                angle: self.angle
                axis: 0, 0, 1
                origin: root.center
        canvas.after:
            PopMatrix

        Image:
            id: img_anim
            source: 'logo.png'
            size_hint: 0,0
            pos_hint: {'center_x': 0.5, 'center_y': 0.5}
    Button:
        text: 'Push Me'
        on_press: root.foo(self)
        size_hint: 0.1,0.1
        pos_hint: {'center_x':0.5, 'center_y':0.5}
''')


class App_container(FloatLayout):
    def __init__(self, **kwargs):
        #Anim
        super(App_container, self).__init__(**kwargs)
        anim = Animation(angle = 360, duration=2)
        anim2 = Animation(size_hint=(2,2), duration=2)

        # rotate the FloatLayout with id "rotate_this"
        anim.start(self.ids["rotate_this"])

        # animate the "img_anim"
        anim2.start(self.ids["img_anim"])
        #Son
        self.sound = SoundLoader.load('zik.wav')
        self.sound.loop = True
        self.sound.play()

    def foo(self, instance, *args):
        self.sound.volume=0


class TestApp(App):

    def build(self):
        return App_container()


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