Skip to content

Instantly share code, notes, and snippets.

@tshirtman
Last active April 5, 2019 02:45
Show Gist options
  • Save tshirtman/7734e3f2b06c550f54f254d4f5d8ea9f to your computer and use it in GitHub Desktop.
Save tshirtman/7734e3f2b06c550f54f254d4f5d8ea9f to your computer and use it in GitHub Desktop.
A simple example of animating a graphical instruction of a widget from a keyboard event.
from kivy.app import App
from kivy.animation import Animation
from kivy.lang import Builder
from kivy.properties import NumericProperty
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
KV = '''
FloatLayout:
MyWidget:
id: widget
size_hint: .5, .5
pos_hint: {'center': (.5, .5)}
<MyWidget>:
canvas.before:
PushMatrix:
Scale:
origin: self.center
xyz: [self.zoom, self.zoom, 1]
canvas:
Line:
rectangle: self.pos + self.size
width: 2
canvas.after:
PopMatrix:
'''
class MyWidget(BoxLayout):
zoom = NumericProperty(1)
class MyApp(App):
def build(self):
self.root = Builder.load_string(KV)
Window.bind(on_key_down=self.key_action)
return self.root
def key_action(self, *args):
widget = self.root.ids.widget
Animation.cancel_all(widget, 'zoom')
anim = (
Animation(zoom=.8, t='out_quad', d=.2)
+ Animation(zoom=1, t='out_elastic', d=.5)
)
anim.start(widget)
if __name__ == '__main__':
MyApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment