Skip to content

Instantly share code, notes, and snippets.

@matham
Created December 30, 2016 05:00
Show Gist options
  • Save matham/0a437c41fcbfcbb2dc9fd5e5f53a78f7 to your computer and use it in GitHub Desktop.
Save matham/0a437c41fcbfcbb2dc9fd5e5f53a78f7 to your computer and use it in GitHub Desktop.
'''
Widget animation
================
This example demonstrates creating and applying a multi-part animation to
a button widget. You should see a button labelled 'plop' that will move with
an animation when clicked.
'''
from os import environ
environ['KIVY_CLOCK'] = 'interrupt'
import kivy
kivy.require('1.0.7')
from kivy.animation import Animation
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
Builder.load_string('''
<MyWidget>:
size_hint: None, None
size: '20dp', '600dp'
canvas:
Color:
rgb: .5, .5, .5
Rectangle:
size: self.size
pos: self.pos
''')
class MyWidget(Widget):
pass
class TestApp(App):
def animate(self, instance):
# create an animation object. This object could be stored
# and reused each call or reused across different widgets.
# += is a sequential step, while &= is in parallel
animation = Animation(pos=(1200, 0), t='linear')
animation += Animation(pos=(0, 0), t='linear')
animation.repeat = True
# apply the animation on the button, passed in the "instance" argument
# Notice that default 'click' animation (changing the button
# color while the mouse is down) is unchanged.
animation.start(instance)
def build(self):
# create a button, and attach animate() method as a on_press handler
button = MyWidget()
self.animate(button)
return button
if __name__ == '__main__':
TestApp().run()
from os import environ
environ['KIVY_CLOCK'] = 'interrupt'
from time import clock
from kivy.uix.label import Label
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
class MyApp(App):
t = clock()
count = 0
label = None
flip = 0
def build(self):
Clock.schedule_interval(self.tick, 0)
Window.fbind('on_flip', self.flippy)
self.label = Label(text='0')
return self.label
def tick(self, *largs):
t = clock()
self.count += 1
self.label.text = str(int(self.label.text) + 1)
if t - self.t >= 1:
print(self.count / (t - self.t), self.count - self.flip)
self.t = t
self.flip = 0
self.count = 0
def flippy(self, *largs):
self.flip += 1
if __name__ == '__main__':
MyApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment