Kivy - множественные анимации и размещение - PullRequest
0 голосов
/ 26 мая 2020

Может ли кто-нибудь помочь мне с размещением анимации и как добавить несколько анимаций? Итак, я получил анимацию круга, который мне нужен, и он работает нормально, но не только мне нужно добавить еще один, и поменьше. Но мне также нужно разместить их в определенном месте c, как показано на эскизе 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()

1 Ответ

1 голос
/ 26 мая 2020

Некоторые проблемы с вашим кодом:

  1. Метод build() (или любой другой метод) может выполнять только один return, поэтому ваш второй return игнорируется.
  2. У вас слишком много аргументов для вашего circle.
  3. origin вашего Rotate, вероятно, за пределами экрана. Я думаю, вы хотите повернуть вокруг центра круга.
  4. super() в Loading2 ссылках 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()

Я добавил root FloatLayout в ваш kv, так что оба класса Loading и Loading2 находятся в вашем GUI.

...