首页 > 解决方案 > Kivy-多个动画和放置

问题描述

谁能帮我放置动画以及如何添加多个动画?所以我得到了我需要的圆圈动画,它工作得很好,但我不仅需要添加另一个更小的动画。但我还需要将它们放置在 PS 草图(https://i.stack.imgur.com/AmKMS.png)上所示的特定位置。所以如果有人可以帮助我,我真的很感激:)

That teh code!
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

Builder.load_string('''                               
<Loading>:
    canvas.before:
        PushMatrix
        Rotate:
            angle: root.angle
            axis: 0, 0, 1
            origin:  1150, 480 
    canvas.after:
        PopMatrix


    canvas:
        Color:
            rgba: .1, 1, .1, .9
        Line:
            width: 2.
            circle:
                (self.center_x, self.center_y, min(1150, 480)
                / 2, 90, 180, 10)
<Loading2>:
    canvas.before:
        PushMatrix
        Rotate:
            angle: root.angle
            axis: 0, 0, 1
            origin:  1150.480
    canvas.after:
        PopMatrix


    canvas:
        Color:
            rgba: .1, 1, .1, .9
        Line:
            width: 2.
            circle:
                (self.center_x, self.center_y, min(1150, 480)
                / 4, 90, 180, 10)`enter code here`
''')

class Loading(FloatLayout):
    angle = NumericProperty(0)
    def __init__(self, **kwargs):
        super(Loading, self).__init__(**kwargs)
        anim = Animation(angle = 360, duration=2)
        anim += Animation(angle = 360, duration=2)
        anim.repeat = True
        anim.start(self)


     def on_angle(self, item, angle):
        if angle == 360:
            item.angle = 0
class Loading2(FloatLayout):
    angle = NumericProperty(0)
    def __init__(self, **kwargs):
        super(Loading, self).__init__(**kwargs)
        anim = Animation(angle = 360, duration=2)
        anim += Animation(angle = 360, duration=2)
        anim.repeat = True
        anim.start(self)

def on_angle(self, item, angle):
        if angle == 360:
            item.angle = 0

class TestApp(App):
    def build(self):
        return Loading()
        return Loading2()

TestApp().run()

标签: pythonkivy

解决方案


您的代码存在一些问题:

  1. build()方法(或任何方法)只能执行一个return,因此您的第二个return将被忽略。
  2. 你的论据太多了circle
  3. origin你的Rotate可能不在屏幕上。我想你想围绕圆心旋转。
  4. super()inLoading2引用而Loading不是Loading2.
  5. 有几个缩进错误(可能只是来自代码的复制/粘贴)。

因此,这是您的代码的修改版本,可以满足您的大部分需求:

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

kv = '''
FloatLayout:
    Loading:
        size_hint: 0.3, 1
    Loading2:
        size_hint: 0.7, 1
        pos_hint: {'right':1}                               
<Loading>:
    canvas.before:
        PushMatrix
        Rotate:
            angle: root.angle
            axis: 0, 0, 1
            origin:  root.center
    canvas.after:
        PopMatrix


    canvas:
        Color:
            rgba: .1, 1, .1, .9
        Line:
            width: 2.
            circle:
                (root.center_x, root.center_y, 100, 90, 180, 10)
<Loading2>:
    canvas.before:
        PushMatrix
        Rotate:
            angle: root.angle
            axis: 0, 0, 1
            origin:  root.center
    canvas.after:
        PopMatrix


    canvas:
        Color:
            rgba: .1, 1, .1, .9
        Line:
            width: 2.
            circle:
                (root.center_x, root.center_y, 200, 90, 180, 10)
'''


class Loading(FloatLayout):
    angle = NumericProperty(0)
    def __init__(self, **kwargs):
        super(Loading, self).__init__(**kwargs)
        anim = Animation(angle = 360, duration=2)
        anim += Animation(angle = 360, duration=2)
        anim.repeat = True
        anim.start(self)


    def on_angle(self, item, angle):
        if angle == 360:
            item.angle = 0


class Loading2(FloatLayout):
    angle = NumericProperty(0)
    def __init__(self, **kwargs):
        super(Loading2, self).__init__(**kwargs)
        anim = Animation(angle = 360, duration=2)
        anim += Animation(angle = 360, duration=2)
        anim.repeat = True
        anim.start(self)

    def on_angle(self, item, angle):
            if angle == 360:
                item.angle = 0


class TestApp(App):
    def build(self):
        return Builder.load_string(kv)

TestApp().run()

我已经FloatLayout为您的. 添加了一个根目录kv,因此LoadingLoading2类都在您的 GUI 中。


推荐阅读