Python | Animation in Kiwi using a .kv file



Kivy — it is a platform independent GUI tool in Python. Since it can run on Android, IOS, Linux, Windows, etc. It is mainly used to develop Android application, but that does not mean that it cannot be used in desktop applications.

Animation:

Animation and AnimationTransition are used to animate the properties of the widget. You must provide at least a property name and a target value. To use animation, follow these steps:

  • Configure the animation object
  • Use the animation object in the widget

To use animation you must have to import:
from kivy.animation import Animation

  Basic Approaches:  1) import runTouchApp 2) import Builder 3) import Widget 4 ) import Animation 5) import Clock 6) import Window 7) import random 8) import listproperty 9) Build the .kv file 10) Create root class 11) Create the clock and anim react class used to animate the boxes 12) Run the App 

In the example below, we create two blocks (red and green) in which we animate the red block, that is, when the application starts, the red block is in its animated form, but to clean up the concepts that I provide a green box that provides random animations per click, meaning it goes anywhere at random. 
The program consists of two main classes. First, there is the clock class for the red frame, since we declare it to be animated, and the animation of the green frame is — in class anim .

To ensure movement, you must play with speed.

Implementation of the approach:

# works the same way as kivy.App is used to launch an application

from kivy.base import runTouchApp

 
# to use the .kv file as a string, we must import it

from kivy.lang import Builder

 
# The widget is the basic building block of GUIs in Kivy.

f rom kivy.uix.widget import Widget

 
# The Clock object allows scheduling
# function call in the future

from kivy.clock import Clock

  
# Animation and Transition Animation
# used to animate widget properties

from kivy.animation import Animation

 
# Property classes are used when
# you create an EventDispatcher.

from kivy.properties import ListProperty

 
# Base class for creating a standard Kivy window.

from kivy.core.window import Window

 
# As the name suggests, used when random thiungs are required

from random import random

 

  
# load kv file as a string

Builder.load_string ( "" "

 

& lt; Root & gt ;:

 
# Set the (start) position of the boxes

  

  ClockRect:

Pos: 300, 300

AnimRect:

  Pos: 500, 300

  
# create and animate a red border
& lt; ClockRect & gt ;:

  canvas:

  Color:

rgba: 1, 0, 0, 1

  Rectangle:

pos: self.pos

size: self.size

 
# create and animate a red border
& lt; AnimRect & gt ;:

canvas:

Color:

rgba: 0, 1, 0, 1

Rectangle:

pos: self.pos

size: self.size

"" " )

 

  
# Create root class

class Root (Widget):

  pass

 

 
# Create a class hours
# how long does it take to animate
# it animates the red block

class ClockRect (Widget):

velocity = ListProperty ([ 10 , 15  ])

 

def __ init __ ( self , * * kwargs):

super (ClockRect, self ) .__ init __ ( * * kwargs)

Clock.schedule_interval ( self . update, 1 / 60. )

 

def update ( self , * args):

self . x + = self . velocity [ 0 ]

self . y + = self .velocity [ 1 ]

 

if self . x & lt;  0 or ( self . x + self . width) & gt; Window.width:

self . velocity [ 0 ] * = - 1

  if self . y & lt;  0 or ( self . y + self . height) & gt; Window.height:

self . velocity [ 1 ] * = - 1

  

 
# Create Animation class
# And add animaton
# Green is animated through this class

class AnimRect (Widget):

  

def anim_to_random_pos ( self ):

Animat ion.cancel_all ( self )

random_x = random () * (Window.width - self . width)

random_y = random () * (Window. height - self . height)

 

anim = Animation (x = random_x, y = random_y,

duration = 4 ,

t = ` out_elastic` )

anim.start ( self )

  

def on_touch_down ( self , touch):

if self . collide_point ( * touch.pos):

self . anim_to_random_pos ()

 
# run the application
runTouchApp (Root ())

Exit :

Animated vedio output: