Last active
July 1, 2019 20:41
-
-
Save me2beats/51ab8a225231aa8f0e9331eba4e12e97 to your computer and use it in GitHub Desktop.
Auto animation?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kivy.app import App | |
from kivy.lang import Builder | |
from kivy.uix.label import Label | |
from kivy.properties import AliasProperty | |
from kivy.animation import Animation as A | |
KV = """ | |
BoxLayout | |
orientation: 'vertical' | |
GridLayout | |
cols: 2 | |
rows: 2 | |
Button | |
text: 'set x =100' | |
on_press: my_label.my_x = 100 | |
Button | |
text: 'set x =-100' | |
on_press: my_label.my_x = -100 | |
Button | |
text: 'set y =100' | |
on_press: my_label.my_y = 100 | |
Button | |
text: 'set y =-100' | |
on_press: my_label.my_y = -100 | |
MyLabel | |
id: my_label | |
x: self.my_x | |
y: self.my_y | |
text : '123456' | |
""" | |
class AnimationProperty(AliasProperty): | |
_data = 0 | |
def __init__(self, **kwargs): | |
kwargs['getter'] = self._getter | |
kwargs['setter'] = self._setter | |
super().__init__(**kwargs) | |
self.instance = kwargs['instance'] | |
self.my_name = kwargs['my_name'] | |
def _getter(self, obj): | |
return self._data | |
def _setter(self, obj, value): | |
if not hasattr(self, 'a'): | |
self.a = A(**{self.name: value}) | |
self.a.fbind('on_complete', self.anim_complete, value) | |
self.a.start(self.instance) | |
else: | |
self._data = value | |
return True # Value changed, dispatch event | |
def anim_complete(self, _,__, value): | |
del self.a | |
class AnimateBhv(): | |
def animate(self, name = '', value = 0, **kwargs): | |
if not name: | |
return | |
instance = kwargs.get('instance', self) | |
value = kwargs.get('value', 0) | |
self.apply_property(**{name: AnimationProperty( | |
value = value, | |
instance = instance, | |
my_name = name | |
)}) | |
class MyLabel(AnimateBhv, Label): | |
def __init__(self, **kwargs): | |
super().__init__(**kwargs) | |
self.animate(name = 'my_x') | |
self.animate(name = 'my_y') | |
class MyApp(App): | |
def build(self): | |
self.root = Builder.load_string(KV) | |
MyApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment