Change language

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, mean ing 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 ( "" "

 

"Root & gt ;:

 
# Set the (start) position of the boxes

  

  ClockRect:

Pos: 300, 300

AnimRect:

  Pos: 500, 300

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

  canvas:

  Color:

rgba: 1, 0, 0, 1

  Rectangle:

pos: self.pos

size: self.size

 
# create and animate a red border
"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)" Window.width:

self . velocity [ 0 ] * = - 1

  if self . y & lt;  0 or ( self . y + self . height)" 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:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically