Animation subclass

Asked by At

in Animation class, how to set d = 0.4, t = 'in_out_quad' by default? Can I use subclass for that?

from kivy.app import App
from kivy.lang import Builder
from kivy.animation import Animation

KV = """
Label
    text: '123'
    on_touch_down: app.test()
"""
class MyApp(App):
    def build(self):
        self.root = Builder.load_string(KV)

    def test(self):
        a = Animation(x = 500, d = .2, t = 'in_out_quad')
        a.start(self.root)

MyApp().run()

Tried something like this (with no success):

from kivy.app import App
from kivy.lang import Builder
from kivy.animation import Animation

KV = """

Label
    text: '123'
    on_touch_down: app.test()

<MyAnim>:
    d: .2
    t: 'in_out_quad'
"""

class MyAnim(Animation):
    pass

class MyApp(App):
    def build(self):
        self.root = Builder.load_string(KV)

    def test(self):
        a = MyAnim(x = 500)
        a.start(self.root)

MyApp().run()

I just want not to write the values of the arguments d and t many times if they are the same in my project

1 Answers

1
John Anderson On Best Solutions

I haven't tested this, but I think you could just put those defaults in your class definition:

class MyAnim(Animation):
    def __init__(self, **kw):
        super(MyAnim, self).__init__(d = .2, t = 'in_out_quad', **kw)

Of course, if you use MyAnim and specify another t= or d=, you will get an error.

That error can be avoided by only adding the default values if they are not already specified:

class MyAnim(Animation):
    def __init__(self, **kw):
        if 'd' not in kw:
            kw['d'] = 0.2
        if 't' not in kw:
            kw['t'] = 'in_out_quad'
        super(MyAnim, self).__init__(**kw)