【问题标题】:How to place a button on a loading animation with Kivy?如何使用 Kivy 在加载动画上放置一个按钮?
【发布时间】:2020-03-31 19:23:10
【问题描述】:

我真的是 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()

【问题讨论】:

  • 您的return 方法中不能有两个return 语句。
  • 我修改了我的代码,但按钮的行为仍然与动画相同
  • 您修改后的代码对我有用。我已经更改了音频和图像文件,但没有其他更改。按下按钮只会使音频静音,并且动画会继续。我在 Ubuntu 18.04 上使用 Kivy v1.11.1 和 Python v3.6.9。你在用什么?
  • Python 3.7.7 和 kivy 1.11.1 win7 按钮不为你旋转?我想要的是我的图像旋转(它是应用程序徽标)和一个静音按钮(因为它正在循环播放),它将停留在
  • 是的,按钮会旋转,但按下按钮只会停止音频。这不是你想要的吗?

标签: button kivy python-3.7


【解决方案1】:

为了使Button 不旋转,它不能处于旋转中的Layout。为此,您可以在App_container 中添加另一个FloatLayout,并且只旋转那个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: 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()

所以FloatLayout 旋转,但Button 不旋转,因为它不在旋转的FloatLayout 内部。

【讨论】:

  • 非常感谢!我对 Kivy 很菜鸟 ^^' 正是我所期待的!我开始更好地理解它是如何工作的!
猜你喜欢
  • 2014-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
  • 1970-01-01
相关资源
最近更新 更多