JohnPaul
JohnPaul

Reputation: 394

Kivy non-GUI events

i am trying to start an animation when the app is first loaded. I.E. immediately after the loading screen has closed. I have tired the "on_enter" event but it doesn't seem to work, any help would be much appreciated.

from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.animation import Animation
from kivy.properties import ListProperty
from kivy.core.window import Window
from random import random
from kivy.graphics import Color, Rectangle

Builder.load_string('''
<Root>:
AnimRect:
    pos: 500, 300
<AnimRect>:
on_enter: self.start_animation
canvas:
    Color:
        rgba: 0, 1, 0, 1
    Rectangle:
        pos: self.pos
        size: self.size
''')

class Root(Widget):
pass

class AnimRect(Widget):
    def anim_to_random_pos(self):
        Animation.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()

    def start_animation(self, touch):
        if self.collide_point(*touch.pos):
             self.anim_to_random_pos()

runTouchApp(Root())

print screen of error

Upvotes: 0

Views: 142

Answers (2)

picibucor
picibucor

Reputation: 773

Does it look like something you wanted to have?

I just deleted the "on_enter" line in your kv, and corrected your indentation.

from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.animation import Animation
from kivy.properties import ListProperty
from kivy.core.window import Window
from random import random
from kivy.graphics import Color, Rectangle

Builder.load_string('''
<Root>:
    AnimRect:
        pos: 500, 300
<AnimRect>:
    canvas:
        Color:
            rgba: 0, 1, 0, 1
        Rectangle:
            pos: self.pos
            size: self.size
''')

class Root(Widget):
    pass

class AnimRect(Widget):
    def anim_to_random_pos(self):
        Animation.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()

    def start_animation(self, touch):
        if self.collide_point(*touch.pos):
             self.anim_to_random_pos()

runTouchApp(Root())

Upvotes: 0

jligeza
jligeza

Reputation: 4693

The on_enter method is defined in the Screen, not in the Widget. You should put that rectangle on a screen (the Root widget should be a screen here), and once the screen's on_enter event fires, start the rectangle animation.

Also, you are calling it incorrectly; function call should contain brackets, i.e. on_enter: self.start_animation()

Upvotes: 1

Related Questions