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

问题描述 投票:0回答:1

我真的是Kivy的新手,我正在尝试制作我的第一个应用程序,但是我不太了解如何使用元素和类...

我试图放一个按钮来停止声音,但它只会停止动画...。

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

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.animation import Animation
from kivy.properties import NumericProperty
from kivy.core.audio import SoundLoader
from kivy.uix.button import Button
from functools import partial
from kivy.uix.boxlayout import BoxLayout

Builder.load_string('''                               
<App_container>: 
    canvas.before:
        PushMatrix
        Rotate:
            angle: root.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}
''')

class App_container(FloatLayout):
    angle = NumericProperty(0)
    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)
        anim.start(self)
        anim2.start(self.ids["img_anim"])
        #Son
        self.sound = SoundLoader.load('zik.wav')
        self.sound.loop = True
        self.sound.play()
        #boutonzik
        btn = Button(text ="Push Me !")
        self.add_widget(btn)
        btn.bind(on_press=partial(self.foo, btn))

    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()
button kivy python-3.7
1个回答
0
投票

为了使Button不旋转,它一定不能在旋转的Layout中。为此,您可以在FloatLayout中添加另一个App_container,并且仅旋转FloatLayout。对您的代码进行以下修改即可:

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: rotate_this.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()
© www.soinside.com 2019 - 2024. All rights reserved.